Login controls in an ASP.NET MVC master page
I have an ASP.NET MVC 2 web site in which I'd like to place login controls in the master page. I want any validation error messages to appear in the master page as well. How would I go about doing this? If I do something like this:
<% using (Html.BeginForm("LogOn", "Account", FormMethod.Post)) { %>
<%= Html.LabelFor(d => d.UserName)%>:
<%= Html.EditorFor(d => d.开发者_开发知识库UserName)%>
<%= Html.LabelFor(d => d.Password)%>:
<%= Html.Password("Password")%>
<input type="submit" value="Login" />
<%= Html.ValidationSummary() %>
<% } %>
...the account controller method gets called just fine. However, when I return View()
from the method, I get an error that no view called "LogOn" exists. How do I achieve my intended result.
When you call the LogOn
action, along with username and password you could pass it a return url to which it will redirect upon successful login instead of returning a view.
I hate to ask the obvious, but it's unclear from your post: do you actually have a "LogOn" view under the "Account" folder in "Views"?
If you simply return View() from an action, it will look for a view with the same name as the action (in this case "LogOn")
hmm... you can just simply return any View from the login controller: How are you going to get the ViewModel to pass on to that view?etc
At best you can get the refferal URL from the request.. then determine the controller/action from that and return a RedirectAction().. but this time with the model errors incorporated.
Two more things I can think of:
Incorporate your User-Login-Control into an iframe. you'll need to gprade your partial view to a full view.
Use a jQuery
$.get(url,null, function(result) { $(loginDiv).html(result);
});
to send the login action, then from the login controller return PartialView("LogOn");
Either way you have a partial page submition and the rest of the content on the page will be unchanged.
精彩评论