在混合和匹配较旧的教程与MVC 3预览1最近的帖子,我遇到了以下问题。我试图移动到我的Fighter
模型(和基础数据库)的JSON驱动编辑,而不是没有JSON的“普通旧版”编辑。JSON/MVC(3P1)HttpPost - 没有得到它在我的EF类工作
我有一个编辑视图(使用Shared EditorTemplate
,fighter.ascx
)设置我的Fighter
类(它存在于EF 4模型中)。
在这我有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 });
}
感谢您的耐心等待,如果您需要额外的代码或信息,我可以提供。
我就可以测试这个今天晚些时候,将让你尽快知道。 TX。 – Tobiasopdenbrouw 2010-08-05 05:41:39
它的工作原理。 TX! – Tobiasopdenbrouw 2010-08-05 07:17:09