在将旧教程与 MVC 3 Preview 1 上的最新帖子混合和匹配时,我遇到了以下问题。我正在尝试对我的Fighter
模型(和底层数据库)进行 JSON 驱动的编辑,而不是没有 JSON 的“普通旧”编辑。
我为我的班级(存在于 EF 4 模型中)设置了一个编辑视图(使用Shared EditorTemplate
, )。fighter.ascx
Fighter
在这我有2个按钮。一个“旧”的,这是一个在没有 JSON 的情况下调用我的编辑控制器的提交,一个是新的,我为此编写了一个新的HttpPost ActionResult
旧按钮有效:新按钮只实现了一半,但我已经可以看到它ActionResult UpdateJsonTrick
没有正确接收来自视图的数据。字符串内容为:“在returnMessage
系统中创建了战斗机''。” 在我对该 ActionResult 做任何有用的事情之前,我必须知道如何传递该数据。我哪里错了?
所以,edit.aspx 只是一个简单的Html.EditorForModel("Fighter")
语句,
但这里是 Fighter.ascx:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Mvc3_EF_BW_Fight.Models.Fighter>" %>
<% using (Html.BeginForm())
{%>
<%: Html.ValidationSummary(true) %>
<script type="text/javascript" src="../../../Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript" src="../../../Scripts/json2.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#JSONTRICK").click(function (event) {
var fighter = { Id: $('#Id').val(),
FighterName: $('#FighterName').val(),
FighterStyleDescription: $('#FighterStyleDescription').val(),
FighterLongDescription: $('#FighterLongDescription').val()
};
$.ajax({
url: '/Barracks/UpdateJsonTrick',
type: "POST",
data: JSON.stringify(fighter),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
// get the result and do some magic with it
var message = data.Message;
$("#resultMessage").html(message);
},
error: function () {
$('#message').html('oops Error').fadeIn();
}
});
return false;
});
});
</script>
<fieldset>
<legend>Fighter template</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Id) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Id) %>
<%: Html.ValidationMessageFor(model => model.Id) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.FighterName) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.FighterName) %>
<%: Html.ValidationMessageFor(model => model.FighterName) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.FighterStyleDescription) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.FighterStyleDescription) %>
<%: Html.ValidationMessageFor(model => model.FighterStyleDescription) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.FighterLongDescription) %>
</div>
<div class="editor-field">
<%: Html.TextAreaFor(model => model.FighterLongDescription) %>
<%: Html.ValidationMessageFor(model => model.FighterLongDescription) %>
</div>
<p>
<input type="submit" value="Save" id="save" />
</p>
<p>
<input type="submit" value="JSONTRICK" id="JSONTRICK" />
<label id="message">
message</label>
</p>
<div>
<span id="resultMessage"></span>
</div>
</fieldset>
<% } %>
这是控制器的(相关位):
[HttpPost]
public ActionResult Edit(int id, FormCollection collection) //Works
{
var fighter = _FightDb.Fighters.Single(f => f.Id == id);
try
{
UpdateModel(fighter);
//var x = ViewData.GetModelStateErrors();
_FightDb.SaveChanges();
return RedirectToAction("Index");
}
catch
{
var viewModel = _FightDb.Fighters.Single(f => f.Id == id); //fighter;
return View(viewModel);
}
}
[HttpPost]
public ActionResult UpdateJsonTrick(Fighter fighter) //doesn't work
{
var x = ViewData.GetModelStateErrors();
string returnMessage = string.Format("Created fighter '{0}' in the system.", fighter.FighterName);
return Json(new PersonViewModel { Message = returnMessage });
}
感谢您的耐心等待,如果您需要其他代码或信息,我可以提供。