Farzaneh Talebi
Farzaneh Talebi

Reputation: 915

Use some PartialView with post action in view?

There is a view for registration that has two PartialView in jquery tab (register by phone or register by email).

enter image description here

I was Called those in Parent view with below codes:

<div id="tabs" class="tabs">
    <nav>
        <ul>
            <li><a href="#section-1" class="fa fa-mobile"><span>موبایل</span></a></li>
            <li><a href="#section-2" class="fa fa-envelope"><span>ایمیل</span></a></li>
        </ul>
    </nav>
    <div class="content">
        <section id="section-1">
            @Html.Partial("_MobileRegister", new S6.ViewModels.MobileRegister())
        </section>
        <section id="section-2">
            @Html.Partial("_EmailRegister" , new S6.ViewModels.EmailRegister())
        </section>
    </div>
</div>

controller and actions:

public ActionResult Register()
{
    return View();
}

public PartialViewResult _MobileRegister()
{
    return PartialView("_MobileRegister");
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult MobileRegister(MobileRegister model)
{
     if (ModelState.IsValid)
     {
     }
     return PartialView("~/Views/Account/_MobileRegister.cshtml", model);
}

Now, when data post to action, if the ModelState is not valid or for any reason that causes the return PartialView to be called, PartialView is shown in seprated page.

enter image description here

What is wrong?

Upvotes: 0

Views: 78

Answers (3)

Ali Mardan
Ali Mardan

Reputation: 167

if you want to render "Partial Views" on the other view, you have to use Ajax instead direct post. Ajax.BeginForm is a good solution.

Upvotes: 1

Andrei Tudorica
Andrei Tudorica

Reputation: 47

If you don't return anything in the

if (ModelState.IsValid)
     {
     }

your funtion will always return the PartialView. Add a return statement in that if.

Upvotes: 2

Related Questions