Jquery 通过AJAX将表单发布到部分视图

Jquery 通过AJAX将表单发布到部分视图,jquery,asp.net-mvc,forms,partial-views,formcollection,Jquery,Asp.net Mvc,Forms,Partial Views,Formcollection,我试图将数据从部分视图表单(viewbag)发布到主控制器上的操作方法。我正在使用Jquery: $.ajax({ url: '@Url.Action("displaypreviewformactionmethod", "MyMainController")', type: 'post', dataType: 'json', data: $('form#realform').serialize(), success: function (data) {

我试图将数据从部分视图表单(viewbag)发布到主控制器上的操作方法。我正在使用Jquery:

$.ajax({
    url: '@Url.Action("displaypreviewformactionmethod", "MyMainController")',
    type: 'post',
    dataType: 'json',
    data: $('form#realform').serialize(),
    success: function (data) {
        $('#DivTagWhereMyPartialViewIs').html(data);
        $('#DivTagWhereMyPartialViewIs').show();
    }
我对表单的局部视图如下所示:

<div id="step-3" class="">
    @using (Html.BeginForm("displaypreviewformactionmethod", "MyMainController"))
    {
        foreach (var frm in Model)
        {
            <tr>
                <td>
                    @Html.LabelFor(model => model.FirstOrDefault().ELabel, frm.SomeText)
                </td>
                <td>
                    @Html.TextBox(frm.FieldName)<br />
                </td>
            </tr>
        }
        <input type="submit" name="Submit" value="Submit" />
    }
</div>
该操作会被调用,但它不会将formcollection中的任何内容传递给部分视图——这让我相信我没有在部分视图中正确标识表单。我尝试在部分视图中的之前添加
,但没有成功。是否有其他方法可以在Jquery中识别表单


非常感谢您的指导。

@AMorrisey我知道您希望通过ajax提交表单。但是我在你的代码中看到了很多建议

1.Razor可以处理ajax表单提交。。不需要编写外部ajax 使用

代替

@使用(Html.BeginForm(“DisplayPreviewFormationMethod”、“MyMainController”))

这里有一篇很好的文章来理解两者之间的区别

2.其次是您的

$.ajax({
    type: 'post',..
由于

单击submit按钮,它将直接点击控制器/操作。所以$ajax部分是无用的

3.动作时应具有适当的模型绑定

public ActionResult displaypreviewformactionmethod(FormCollection form)

should have been

public ActionResult displaypreviewformactionmethod(SomeModel model)

您的表单没有
id=“realform”
——但是您可以使用
@添加它,使用(Html.BeginForm(“displayPreviewFormationMethod”,“MyMainController”,FormMethod.Post,new{id=“realform”}))
。是什么触发了ajax调用?而且您的
foreach
循环无论如何都无法正确绑定-引用部分视图不是强类型的原因是什么?这将更容易将您的模型发布到控制器..局部视图具有模型。。。。OP只放了部分视图。。。控制器不应该有一个formcollection@StephenMuecke-你的答案和我想做的一样:(Html.BeginForm(“displaypreviewformactionmethod”,“MyMainController”,FormMethod.Post,new{id=“realform”}))我会将它标记为答案,但不能,因为它是一个回复。显然,
$.ajax()
是有效的,因为OP已经声明了它的命中方法(并且建议使用
Ajax.BeginForm()
而不是
$。Ajax()
是一个糟糕的建议)感谢Raja-我不能使用模型,因为表单内容(字段和字段值)是动态生成的,因此使用ViewBag。
$.ajax({
    type: 'post',..
public ActionResult displaypreviewformactionmethod(FormCollection form)

should have been

public ActionResult displaypreviewformactionmethod(SomeModel model)