3

我试图在这里遵循一些好的帖子来让它工作,但是每次我单击 ajax 回发时,我的控制器中的断点都会显示一个具有空值的模型。此页面显示模型值仅供查看,但我尝试将它们放在自己的形式中,并将它们包装在 ajax 形式中,但似乎没有任何效果。

@model VendorProfileIntranet.Models.VendorProfile

$(function () {
$("form").submit(function () {
    if ($(this).valid()) {
        $.ajax({
            url: this.action,
            type: this.method,
            data: $(this).serialize(),
            success: function (result) {
                $("#message").html(result);
            }
        });
    }
    return false;
});

});

    @using (Ajax.BeginForm("SelectVendor", "Home", new AjaxOptions { HttpMethod="post", InsertionMode=InsertionMode.Replace, UpdateTargetId="message" }))
{
    <div style="float:right; width:500px">
        <div id="message"></div>
        <input id="btnSubmit" type="submit" value="Select Vendor" />
    </div>

视图真的很长(仅显示模型值以供查看)在这里缩写。

<div id="viewform">
    <div id="viewform-contact" style="float:left;">
    <fieldset>
        <legend>Contact Information</legend>
        @Html.HiddenFor(model => model.ProfileID)
        <div class="view-field">
            @Html.LabelFor(model => model.Name)
            @Html.DisplayFor(model => model.Name)
        </div>
        <br />
        <div class="view-field">
            @Html.LabelFor(model => model.Email)
            @Html.DisplayFor(model => model.Email)
        </div>
        <br />
        <div class="view-field">
            @Html.LabelFor(model => model.Phone)
            @Html.DisplayFor(model => model.Phone)
        </div>
        <br />
        <div class="view-field">
            @Html.LabelFor(model => model.Website)
            @Html.DisplayFor(model => model.Website)
        </div>
        <br />
        <div class="view-field">
            @Html.LabelFor(model => model.CompanyName)
            @Html.DisplayFor(model => model.CompanyName)
        </div>
        <br />
        <div class="view-field">
            @Html.LabelFor(model => model.Address1)
            @Html.DisplayFor(model => model.Address1)
        </div>
        <br />
        <div class="view-field">
            @Html.LabelFor(model => model.Address2)&nbsp;
            @Html.DisplayFor(model => model.Address2)
        </div>
        <br />
        <div class="view-field">
            @Html.LabelFor(model => model.City)
            @Html.DisplayFor(model => model.City)
        </div>
        <br />
        <div class="view-field">
            @Html.LabelFor(model => model.State)
            @Html.DisplayFor(model => model.State)
        </div>
        <br />
        <div class="view-field">
            @Html.LabelFor(model => model.Zip)
            @Html.DisplayFor(model => model.Zip)
        </div>
        <br />
        <div class="view-fieldwide">
            @Html.LabelFor(model => model.VendorNotes)
        </div>
        <br />
        <div class="view-fieldwide">
            @Html.DisplayFor(model => model.VendorNotes)
        </div>
        <br /><br />
        </fieldset>
    </div>
}

控制器。我已经以一种或两种形式尝试了上述方法。使用上面的代码,模型包含所有空值,除了填充在隐藏输入字段中的 ProfileID。我认为传递模型应该可以工作,而不必为每个模型值创建一个隐藏字段?

[HttpPost]
        public ActionResult SelectVendor(VendorProfile pageModel)
    {
        // handle selected vendor
            _DAL.SelectVendor(pageModel.ProfileID);
            return Content("This Vendor has been selected", "text/html");
    }

回答

由于使用 DisplayFor,模型值未绑定

4

1 回答 1

7

如评论中所述,您实际上希望在您的模型中看到您使用DisplayFor. DisplayFor字段不会发布,它们只是为了显示。为了将这些值发回,您需要HiddenFor为每个要发布的字段提供一个,以便模型绑定能够正常工作。

于 2013-03-15T21:10:09.307 回答