ASP.NET MVC +的ModelState和局部视图 [英] ASP.NET MVC + modelstate and partial view
问题描述
我有一个名为登录时局部视图,我基本上是复制的登录投入控制。我使用Html.RenderPartial放置控制在我的Ajax.BeginForm
I have a partial view called LogOn where i basically copied the logon inputs into a control. I am using Html.RenderPartial to place the control in my Index.Html inside of an Ajax.BeginForm
<div id="login_ajaxtarget">
<% using (Ajax.BeginForm("Logon", "Account", new AjaxOptions { UpdateTargetId = "login_ajaxtarget", HttpMethod = "Post" })) { %>
<% Html.RenderPartial("LogOn"); %>
<% } %>
</div>
我想传回的验证消息,让他们展示,但我似乎无法得到它的工作。我传递模型的看法,但它似乎没有正确显示验证。
I am trying pass back the validation messages and have them display but i cant seem to get it to work. I am passing the model to the view but it doesnt seem to render the validation correctly.
我的控制器
public ActionResult LogOn(string userName, string password, bool rememberMe, string returnUrl)
{
if (!ValidateLogOn(userName, password))
{
return PartialView("LogOn", ModelState);
//return RedirectToAction("Index", "Home");
}
FormsAuth.SignIn(userName, rememberMe);
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
我的局部视图
<%= Html.ValidationSummary("Login was unsuccessful. Please correct the errors and try again.") %>
<div>
<fieldset>
<legend>Account Information</legend>
<p>
<label for="username">Username:</label>
<%= Html.TextBox("username") %>
<%= Html.ValidationMessage("username") %>
</p>
<p>
<label for="password">Password:</label>
<%= Html.Password("password") %>
<%= Html.ValidationMessage("password") %>
</p>
<p>
<%= Html.CheckBox("rememberMe") %> <label class="inline" for="rememberMe">Remember me?</label>
</p>
<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
我想使用的建议,从这个线程(的http://论坛.asp.net / P / 1398814 / 3023892.aspx#3023892 ),但林不知道这是否是正确的。我真正想要的是能够把,而不必导航到一个新的页面,以使用它的主页上登录功能。如果有一个更简单的方法来做到这一点而言,我所有的耳朵!在此先感谢。
I am trying to use the suggestions from this thread ( http://forums.asp.net/p/1398814/3023892.aspx#3023892 ) but im not sure if this is correct. All I really want is to be able to place the LogOn capability on the home page instead of having to navigate to a new page in order to use it. If there is an easier way to do this, Im all ears! thanks in advance.
推荐答案
伙计真的很抱歉。我还没有看到默认MVCApplication项目模板。
Hey man really sorry. I haven't seen the default MVCApplication project template.
检查它可能会解决你的问题。
Check it out may solve your problem.
控制:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<%= Html.ValidationSummary("Login was unsuccessful. Please correct the errors and try again.") %>
<% using (Ajax.BeginForm("LogOn", "Account", new AjaxOptions() { UpdateTargetId = "UpdatePanel" }))
{ %>
<div id="UpdatePanel">
<fieldset>
<legend>Account Information</legend>
<p>
<label for="username">
Username:</label>
<%= Html.TextBox("username") %>
<%= Html.ValidationMessage("username") %>
</p>
<p>
<label for="password">
Password:</label>
<%= Html.Password("password") %>
<%= Html.ValidationMessage("password") %>
</p>
<p>
<%= Html.CheckBox("rememberMe") %>
<label class="inline" for="rememberMe">
Remember me?</label>
</p>
<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
<% } %>
在code:
The code:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult LogOn(string userName, string password, bool rememberMe, string returnUrl)
{
if (!ValidateLogOn(userName, password))
{
return PartialView("Login");
}
FormsAuth.SignIn(userName, rememberMe);
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
这对我的作品。如果问题仍然存在,检查响应并要求萤火虫。
希望这有助于
这篇关于ASP.NET MVC +的ModelState和局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!