JSON / MVC(3P1)HttpPost - 不是让工作在我的EF类
-
25-09-2019 - |
题
在混合并与MVC 3最近帖子匹配的旧教程预览1,I碰上以下问题。我想搬到我Fighter
模型的JSON驱动的编辑(和潜在的DB),而不是“普通的旧”的编辑没有JSON。
我有一个编辑视图(其使用Shared EditorTemplate
,fighter.ascx
)设置为我Fighter
类(它存在于一个EF 4模型)。
在此我有2个按钮。一个“老”之一,这是一个提交调用我的editcontroller没有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 });
}
感谢您的耐心,如果你需要额外的代码或信息,我可以提供。
解决方案
有一个在MVC 3预览1其中JsonValueProviderFactory
默认未注册的错误。
有这样的事情在你的Global.asax
应该有所帮助:
ValueProviderFactories.Factories.Add(new JsonValueProviderFactory())
不隶属于 StackOverflow