0
變化我有一個ASP MVC Web應用程序。我的UI並不反映我的模型
我有一個部分的形式和它initally顯示「您好安迪!」。 我按下提交按鈕,我把它改成'Hello Andy Again !. 我將模型傳遞迴UI。 該標籤仍顯示舊值。
爲什麼?
我的標記:
@using (Ajax.BeginForm("SaveAlertPreferences", "Users", new AjaxOptions
{
UpdateTargetId = "partialform",
InsertionMode = InsertionMode.Replace,
HttpMethod = "POST",
{
<div>
@Html.AntiForgeryToken()
<div class="section group">
<div class="col span_3_of_12">
@Html.LabelFor(model => model.myStub)
</div>
<div class="col span_9_of_12">
@Html.TextBoxFor(model => model.myStub)
</div>
</div>
<div class="section group">
<div class="col span_3_of_12">
</div>
<div class="col span_4_of_12">
<input type="submit" value="Press me" />
</div>
<div class="col span_5_of_12">
</div>
</div>
</div>
}
我的模型:
public class ChangeAlertPreferencesModel
{
public string myStub { get; set; }
}
我的控制器:
[AcceptVerbs("HEAD", "GET")]
public PartialViewResult _ChangeAlertPreferences()
{
Response.CacheControl = "no-cache";
ChangeAlertPreferencesModel m = new ChangeAlertPreferencesModel();
m.myStub = "Hello Andy!";
return PartialView("_ChangeAlertPreferences", m);
}
[HttpPost]
public PartialViewResult SaveAlertPreferences(ChangeAlertPreferencesModel m)
{
Response.CacheControl = "no-cache";
if (ModelState.IsValid)
{
m.myStub = "Hello Andy Again!";
return PartialView("_ChangeAlertPreferences", m);
}
else
{
m.myStub = "I have errored!";
return PartialView("_ChangeAlertPreferences", m);
}
return null;
}
1:是'partialform'標籤的父'div'的id?第二:如果你沒有在你的項目中引用jquery-ajax-unobtrusive.js,那麼ajax表單和其他ajax功能「missbehave」,所以確保你明白。你可以通過NuGet下載它。 – Spluf
@ Spluf嗨,謝謝你的時間。是partialform是表單父級的ID。我會看看這個框架 - 謝謝 –
@CodeCaster謝謝。將看看 –