C# 如何在视图中设置按钮以在MVC 5中保存编辑的实体
您好,我目前有一个使用MVC5和EntityFramework6的应用程序。我有一个当前代码的视图:C# 如何在视图中设置按钮以在MVC 5中保存编辑的实体,c#,asp.net-mvc,razor,entity-framework-6,C#,Asp.net Mvc,Razor,Entity Framework 6,您好,我目前有一个使用MVC5和EntityFramework6的应用程序。我有一个当前代码的视图: <tr> <td class="infoCell">@Html.LabelFor(x => Model.PersonInfo.firstName, "First Name:")</td> <td class="infoCell">@Html.EditorFor(x => Model.PersonInfo.firstName)&l
<tr>
<td class="infoCell">@Html.LabelFor(x => Model.PersonInfo.firstName, "First Name:")</td>
<td class="infoCell">@Html.EditorFor(x => Model.PersonInfo.firstName)</td>
<td class="infoCell">@Html.LabelFor(x => Model.PersonInfo.lastName, "Last Name: ")</td>
<td class="infoCell">@Html.EditorFor(x => Model.PersonInfo.lastName)</td>
</tr>
此控制器正在调用我的服务方法Save,代码为:
public void Save(PersonInfo PersonInfo, PersonModeInfo PersonModeInfo, PersonISS PersonISS, Injury Injury)
{
using (MEDICSContext db = new MEDICSContext())
{
db.Entry(PersonInfo).State = EntityState.Modified;
db.Entry(PersonModeInfo).State = EntityState.Modified;
db.Entry(PersonISS).State = EntityState.Modified;
db.Entry(Injury).State = EntityState.Modified;
db.SaveChanges();
}
}
我想在视图中设置一个按钮,在不离开视图的情况下保存我在“editorFor”字段中所做的任何更改。我不完全确定如何将按钮绑定到控制器中的操作,以及如何在不重新加载页面的情况下更新页面。非常感谢您的任何帮助。通过jquery Ajax调用来实现这一点。在页面脚本中为您添加事件按钮
$("#myButton").click(function () {
$.ajax({
type: "POST",
url: "/Save",
data: $('#myForm').serialize(),
datatype: "html",
success: function (data) {
alert('saved !');
}
});
}))
您需要在页面上有一个名为#myForm的表单或任何您想称之为的表单。使用AJAX提交数据。下面的示例使用jQuery 1.7.1。将此函数附加到客户端单击处理程序
$.ajax({
url: '@Url.Action("SomeAction", "SomeController")',
type: 'POST',
data: GlobalJsHelpers.getFormData($('form')),
success: function(resultData) {
// POST was sucessful, do something
}
});
数据
必须是表单的JSON数据,您可以使用以下帮助程序:
var GlobalJsHelpers = GlobalJsHelpers || {};
GlobalJsHelpers.getFormData = function (forms) {
var formElements = GlobalJsHelpers.getFormDataElements(forms);
return GlobalJsHelpers.createJsonObjectFromElements(formElements);
};
GlobalJsHelpers.getFormDataElements = function (forms) {
// find all input, textarea, select and button elements which are not disabled
// and if they are a checkbox they must be checked to be included (required due to mvc checkbox behaviour with hidden field)
// and if there are radio buttons, only take the :checked radiobutton
return $(forms).find(':input[name]:not(:disabled)').filter(':not(:checkbox, :radio), :checked');
};
GlobalJsHelpers.createJsonObjectFromElements = function(formElements) {
// create array of objects (in a style understandable by the MVC modelbinder)
if (formElements.length === 0)
return ({});
return formElements.map(function () {
var input = $(this);
return { name: input.attr('name'), value: input.val() };
});
};
看一看,这可能就是你想要的我正在使用“@using(ajax.beginform(“save”,“people”,new-ajaxoptions(){httpmethod=“POST”},new{id=model.personInfo.personNumber}”)”是否有办法将id添加到razor标记中,以便我可以将其添加到#myForm?您建议我如何将数据字段添加到#myForm?如果您的数据被html表单标记包围,则该表单标记中的每个输入都将发布到控制器。数据应该通过直接绑定映射到您的viewmodel。或者您可以简单地使用
数据:$('form')。serialize(),
var GlobalJsHelpers = GlobalJsHelpers || {};
GlobalJsHelpers.getFormData = function (forms) {
var formElements = GlobalJsHelpers.getFormDataElements(forms);
return GlobalJsHelpers.createJsonObjectFromElements(formElements);
};
GlobalJsHelpers.getFormDataElements = function (forms) {
// find all input, textarea, select and button elements which are not disabled
// and if they are a checkbox they must be checked to be included (required due to mvc checkbox behaviour with hidden field)
// and if there are radio buttons, only take the :checked radiobutton
return $(forms).find(':input[name]:not(:disabled)').filter(':not(:checkbox, :radio), :checked');
};
GlobalJsHelpers.createJsonObjectFromElements = function(formElements) {
// create array of objects (in a style understandable by the MVC modelbinder)
if (formElements.length === 0)
return ({});
return formElements.map(function () {
var input = $(this);
return { name: input.attr('name'), value: input.val() };
});
};