我一直非常不成功,讓這個工作!ASP.net MVC3 - Razor視圖和部分視圖與Ajax回傳
在視圖中......
@model Project.Models.Account.ForgotPasswordModel
@{
ViewBag.Title = "Forgot Password";
}
<h2>ForgotPassword</h2>
<span id='@ViewBag.ReplaceID'>
@Html.Partial("_ForgotPasswordUserNameAjax", ViewData.Model)
</span>
我使這個partialView ...
@model Project.Models.Account.ForgotPasswordModel
@{
this.Layout = null;
}
@using (Ajax.BeginForm("ForgotPassword", new AjaxOptions() { UpdateTargetId = ViewBag.ReplaceID, InsertionMode = InsertionMode.InsertAfter }))
{
@Html.ValidationSummary(true, "Forgot Password was unsuccessful. Please correct the errors and try again.")
<div id="login" class="box">
<fieldset>
<h2>Account Information</h2>
<div class="inside">
<div class="editor-label">
@Html.LabelFor(m => m.Username)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.Username)
<br />
@Html.ValidationMessageFor(m => m.Username)
<br />
</div>
<p>
<input type="submit" value='Submit' />
</p>
</div>
</fieldset>
</div>
}
而這個控制器動作......
[HttpPost]
public PartialViewResult ForgotPassword(ForgotPasswordModel model)
{
if (String.IsNullOrEmpty(model.Username))
{
ModelState.AddModelError("Username", ForgotPasswordStrings.USER_NAME_REQUIRED);
}
else
{
bool isGood = false;
model.Question = this._security.ValidateUserNameGetSecurityQuestion(model.Username, out isGood);
if (!isGood)
{
ModelState.AddModelError("Username", ForgotPasswordStrings.USER_NAME_INVALID);
}
}
PartialViewResult retVal = null;
if (ModelState.IsValid)
{
retVal = PartialView("ForgotPasswordAnswerAjax", model);
}
else
{
retVal = PartialView("_ForgotPasswordUserNameAjax", model);
}
return retVal;
}
然而,每一次,視圖只返回PartialView,不包含在佈局中(所以只是我的PartialView在屏幕上,沒有別的)。我試過了af EW事情我已經在網上找到... http://www.compiledthoughts.com/2011/01/aspnet-mvc-razor-partial-views-with.html http://stackoverflow.com/questions/4655365/mvc3-submit-ajax-form
但是,什麼也修復了這個問題。我已將InsertionMode更改爲所有值而不更改。我已將@ Html.Partial更改爲代碼塊,如 @ { Html.RenderPartial(「_ ForgotPasswordUserNameAjax」,ViewData.Model); }。
這不起作用...
我用盡了想法(和耐心)!
請幫忙!
嗯,我從MVC2應用程序除了將這個項目MVC3超過剃刀改變了看法。在切換到MVC3之前,這些工作非常好。我期望不必更改控制器代碼(除了帶下劃線的視圖的新命名)。 ( – DavidAndroidDev 2011-02-03 16:35:01