我不太了解如何使用 Ajax.BeginForm 以及与我的控制器的交互。这是我的情况。我有一个包含项目列表的页面。当我单击项目旁边的编辑时,会打开一个 jquery 对话框,其中包含要编辑的字段的强类型部分视图。当我单击更新时,如果视图模型有效,我想保存我的更改,或者在必要时显示错误。我的问题是当我单击更新时。我无法将我的序列化数据作为视图模型发送到控制器。
编辑:好的,我发现了 1 个问题。我的表单标签没有正确形成,所以表单字段在表单之外。现在我对表单的序列化有疑问。我收到这条消息:无效的 JSON 原语:section_id。
这是我的序列化字符串:
section_id=74§ion_name_en=The+CF+Mission§ion_name_fr=La+mission+des+FC&norm_tab_name_en=介绍&norm_tab_name_fr=介绍§ion_tab_id=2&active=true&active=false&display_order=100&banner=banner.jpg&tab_title_en=介绍&tab_title_fr=介绍&content_text%3B%3B%%3E%2n %2Fdiv%3E%0D%0A%0D%0A%3Cdiv%3E%3Cstrong%3E+任务+的+加拿大+部队+是+到%3A%3C%2Fstrong%3E%3C%2Fdiv%3E%0D %0A%0D%0A%3Cul+style%3D%22list-style-type%3A+disc%22%3E%0D%0A%09%3Cli%3Eprotect+Canada%2C%3C%2Fli%3E%0D%0A %09%3Cli%3E防御+北+美洲+在+合作+与+美国%2C+和%3C%2Fli%3E%0D%0A%09%3Cli%3E贡献+到+国际+和平+和+安全。%3C%2Fli%3E%0D%0A%3C%2Ful%3E%0D%0A%0D%0A%3Cdiv%3E%3Cbr+%2F%3E%0D%0AOn+any+given+day%2C+about+8 %2C000+加拿大+部队+成员+-+一个+三分之一+我们的+可部署+部队+-+正在+准备+为%2C+参与+在+或+返回+从+一个+海外+任务。+在+首页%2C+加拿大+部队+可以+带来++最好的+可用+军事+资源+从+跨越+加拿大+到+承担+在+a+危机+或+威胁%2C+任何地方+它+发生% 2C+全国范围。%3Cbr+%2F%3E%0D%0A%3Cbr+%2F%3E%0D%0AThe+Canadian+Forces+now+has+an+agile%2C+high-readiness+Special+Operations+Forces +能够+的+操作+跨越++频谱+冲突+在+国内+和+国外%3B+它+是+包括+的+联合+任务+部队+2+(JTF-2)%2C +the+Special+Operations+Aviation+Squadron%2C+the+Joint+Nuclear%2C+Biological+and+Chemical+Defence+Company%2C+and+the+Canadian+Special+Operations+Regiment.%3C%2Fdiv% 3E%0D%0A&content_text_fr=%3Cdiv%3E%26nbsp%3B%3C%2Fdiv%3E%0D%0A%0D%0A%3Cdiv%3E%3Cstrong%3ELa+mission+des+Forces+canadiennes+est+de%3C%2Fstrong%3E %3C%2Fdiv%3E%0D%0A%0D%0A%3Cul%3E%0D%0A%09%3Cli%3Eprot%26eacute%3Bger+le+加拿大%2C%3C%2Fli%3E%0D%0A%09 %3Cli%3Ed%26eacute%3Bfendre+l%26%2339%3BAm%26eacute%3Brique+du+Nord+en+collaboration+avec+les+%26Eacute%3Btats-Unis+et%3C%2Fli%3E%0D%0A %09%3Cli%3Ede+favoriser+la+paix+et+la+s%26eacute%3Bcurit%26eacute%3B+dans+le+monde.%3C%2Fli%3E%0D%0A%3C%2Ful%3E% 0D%0A%0D%0A%3Cdiv%3E%3Cbr+%2F%3E%0D%0AChaque+jour%2C+environ+8+000+militaires+des+Forces+canadiennes+(FC)%2C+soit+%26agrave%3B +peu+pr%26egrave%3Bs+le+tiers+de+nos+forces+d%26eacute%3Bployables%2C+se+pr%26eacute%3Bparent+%26agrave%3B+une+mission+%26agrave%3B+l%26 %2339%3B%26eacute%3Btranger%2C+y+参与者+sur+place+ou+en+reviennent。+Au+pays%2C+les+Forces+canadiennes+peuvent+mettre+%26agrave%3B+profit+les+meilleures+ressources+militaires+de+partout+au+加拿大%2C+en+cas+de+crise+ou +de+menace%2C+n%26%2339%3Bimporte+o%26ugrave%3B+sur+l%26%2339%3Bensemble+du+territoire。%3Cbr+%2F%3E%0D%0A%3Cbr+%2F%3E%0D%0ALes+Forces+canadiennes+se+sont+dot%26eacute%3Bes+d%26%2339%3Bune+capacit%26eacute%3B+d %26%2339%3Bop%26eacute%3Brations+sp%26eacute%3Bciales+%26eacute%3Blargie+et+am%26eacute%3Blior%26eacute%3Be+能力+d%26%2339%3Bintervenir+rapidement+et+pouvant+mener +des+op%26eacute%3Brations+sur+tout+l%26%2339%3B%26eacute%3Bventail+des+conflits%2C+au+加拿大+comme+%26agrave%3B+l%26%2339%3B%26eacute %3Btranger+%3B+ces+forces+comprennent+la+Deuxi%26egrave%3Bme+Force+op%26eacute%3Brationnelle+interarm%26eacute%3Bes+(FOI-2)%2C+l%26%2339%3BEscadron+d% 26%2339%3Bop%26eacute%3Brations+sp%26eacute%3Bciales+(航空)%2C+la+Compagnie+interarm%26eacute%3Bes+de+d%26eacute%3Bfense+nucl%26eacute%3Baire%2C+biologique+et +chimique+et+le+R%26eacute%3Bgiment+d%26%2339%3Bop%26eacute%3Brations+sp%26eacute%3Bciales+du+加拿大.%3C%2Fdiv%3E%0D%0A%3C%2Fdiv%3E%0D%0A%3C%2Fdiv%3E%0D%0A
这是我的模型定义:
public class SectionContent
{
private int _section_tab_id;
private int _section_id;
private string _section_name_en;
private string _section_name_fr;
private string _tab_title_en;
private string _tab_title_fr;
private int _display_order;
private string _norm_tab_name_en;
private string _norm_tab_name_fr;
private string _banner;
private bool _active;
private int _content_id;
private string _content_text_en;
private string _content_text_fr;
[snip]
}
这是我的部分视图:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ForcesCMS.Models.SectionContent>" %>
<%=Html.ValidationSummary(MyLabels.labels.please_correct_markedfields, new { @class = "validation" })%>
<fieldset>
<legend><%=Model.tab_title_en%></legend>
<input type="hidden" id="section_id" name="section_id" value="<%= Model.section_id%>" />
<input type="hidden" id="section_name_en" name="section_name_en" value="<%= Model.section_name_en%>" />
<input type="hidden" id="section_name_fr" name="section_name_fr" value="<%= Model.section_name_fr%>" />
<input type="hidden" id="norm_tab_name_en" name="norm_tab_name_en" value="<%= Model.norm_tab_name_en%>" />
<input type="hidden" id="norm_tab_name_fr" name="norm_tab_name_fr" value="<%= Model.norm_tab_name_fr%>" />
<input type="hidden" id="content_id" name="content_id" value="<%= Model.content_id%>" />
<div class="formrowFull">
<div class="formcol">
<label for="section_tab_id" class="inline"><%=MyLabels.labels.id%></label>
<%= Html.TextBoxFor(model => model.section_tab_id, new { @class = "smallBoxReadonly" })%>
</div>
<div class="formcol">
<label for="active" class="inline"><%=MyLabels.labels.active%></label>
<%=Html.CheckBoxFor(model => model.active)%>
</div>
</div>
<div class="formrowFull">
<div class="formcol">
<label for="display_order" class="inline mandatory"><%=MyLabels.labels.display_order%></label>
<%= Html.TextBoxFor(model => model.display_order, new { @class = "smallBox" })%>
<%= Html.ValidationMessageFor(model => model.display_order)%>
</div>
<div class="formcol">
<label for="banner" class="inline"><%=MyLabels.labels.banner%></label>
<%= Html.TextBoxFor(model => model.banner, new { @class = "largeBox" })%>
</div>
</div>
<div class="formrowFull">
<div class="formcol">
<label for="tab_title_en" class="mandatory"><%=MyLabels.labels.title_en%></label>
<%= Html.TextBoxFor(model => model.tab_title_en, new { @class = "largeBox" })%>
<%= Html.ValidationMessageFor(model => model.tab_title_en)%>
</div>
<div class="formcol">
<label for="tab_title_fr" class="mandatory"><%=MyLabels.labels.title_fr%></label>
<%= Html.TextBoxFor(model => model.tab_title_fr, new { @class = "largeBox" })%>
<%= Html.ValidationMessageFor(model => model.tab_title_fr)%>
</div>
</div>
<div class="formrowFull">
<div class="formcol">
<input type="hidden" name="content_id" id="content_id" value="<%=Model.content_id %>" />
<%= Html.LabelFor(model => model.content_text_en)%>
<%= Html.TextAreaFor(model => model.content_text_en, new { @class = "ckeditor_tpl" })%>
<%= Html.ValidationMessageFor(model => model.content_text_en)%>
</div>
<div class="formcol">
<%= Html.LabelFor(model => model.content_text_fr)%>
<%= Html.TextAreaFor(model => model.content_text_fr, new { @class = "ckeditor_tpl" })%>
<%= Html.ValidationMessageFor(model => model.content_text_fr)%>
</div>
</div>
</fieldset><%}%>
接下来我将发布我的控制器和脚本,这些是让我感到困惑的部分。
控制器:
[HttpGet]
// Called when click on Edit link - data displayed in modal dialog
public PartialViewResult AddEditRecord(int id)
{
ViewData["IsUpdate"] = true;
SectionContent sc = ContentHandler.GetTabContent(id);
return PartialView("ContentForm", sc);
}
[HttpPost]
[ValidateInput(false)]
// Called when clicking on Update in modal
public PartialViewResult AddEditRecord(SectionContent sc, string cmd)
{
// Never valid because model (sc) is always null
if (ModelState.IsValid)
{
try
{
//update
int result = ContentHandler.UpdateSectionContent(sc);
return PartialView("ContentForm");
}
catch { }
}
if (Request.IsAjaxRequest())
{
return PartialView("ContentForm", sc);
}
else
{
return PartialView("ContentForm");
}
}
脚本:
$.ajaxSetup({ cache: false });
function DoAjaxUpdate() {
$.ajax({
url: $("#frmEdit").attr("action"),
type: 'POST',
dataType: "json",
contentType: 'application/json',
data: $("#frmEdit").serialize(),
success: function (result) {
$('#dialog-edit').html(result);
}
});
}
$(".editDialog").live("click", function (e) {
var url = $(this).attr('href');
$("#dialog-edit").dialog({
title: 'Edit Tab and Content',
autoOpen: false,
resizable: false,
height: 800,
width: 600,
show: { effect: 'drop', direction: "up" },
modal: true,
draggable: true,
open: function (event, ui) {
$(this).load(url);
},
close: function (event, ui) {
$(this).dialog('close');
},
buttons: {
"Update": function () {
DoAjaxUpdate();
},
"Cancel": function () {
$(this).dialog('close');
}
}
});
$("#dialog-edit").dialog('open');
return false;
});
如何正确序列化表单,以便控制器将其识别为有效的 ViewModel。那么一旦解决了,我应该在我的控制器操作中做什么?json结果?返回部分视图?行动结果?
我一直在浏览大量的 SO 帖子以及在其他网站上,过去几天我尝试了很多东西,但没有运气。任何帮助表示赞赏。谢谢 !