Partial view Shows its action in the URL instead of the container view action
I have a partial view which contains a form, and this partial view exists in a view which contain some other forms and html.
When I press submit and the validation fails, it show this partial view form action in the URL instead of the original URL.Parent View "User Account": - Login partial view - Register partial view
Original URL when the page open is: / users/account
URL when register validation fail become: /users/registerHere is my partial view:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<PicGhost.Website.ViewModels.RegisterViewModel>" %>
<% using (Html.BeginForm("Register", "Users", FormMethod.Post)) {%>
开发者_开发百科 <%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.UserName) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.UserName) %>
<%: Html.ValidationMessageFor(model => model.UserName) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Email) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Email) %>
<%: Html.ValidationMessageFor(model => model.Email) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Password) %>
</div>
<div class="editor-field">
<%: Html.PasswordFor(model => model.Password)%>
<%: Html.ValidationMessageFor(model => model.Password) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.ConfirmPassword) %>
</div>
<div class="editor-field">
<%: Html.PasswordFor(model => model.ConfirmPassword) %>
<%: Html.ValidationMessageFor(model => model.ConfirmPassword) %>
</div>
<p>
<input type="submit" value="Register" />
</p>
</fieldset>
<% } %>
And register action:
[HttpPost]
public ActionResult Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
IUser user = _factory.CreateUser(model.UserName, model.Email, model.Password);
UserRepository.Add(user);
return RedirectToAction("Index");
}
return View(model);
}
How to avoid showing this wrong URl and keep the original URL?
Original URL:
After validation URL:
You post to /users/register and that is what is returning the response.
To avoid this either:
- post to the action that created the page - perhaps not possible if you have several forms
- or rather than returning the view return a redirecttoaction to the original action
If you redirect to an action though you will need to handle the model data, perhaps from several child actions - you can pass the data back in the TempData collection for the redirect.
You need to return the containing view. What is happening is that because modelstate is invalid, it is returning only the partial view. This means that somehow, you will have to get the modelstate of your partial view back into the partial view. I did this once by having my parent view have a model that contained the child models.
Something like this
public class ParentViewModel
{
public RegisterViewModel RegisterModel { get; set; }
}
Then in the Account view
<% Html.RenderPartial("Register", Model.RegisterModel); %>
Then you can build the ParentViewModel and dump in the RegisterViewModel and then return the Account view when ModelState is invalid. Inside the Register action you'd have something like this.
if (ModelState.IsValid)
{
...
}
var parentModel = new ParentViewModel()
{
RegisterModel = model;
};
return View("Account", parentModel);
Just make sure you always init RegisterModel or you'll get an error.
精彩评论