通过Ajax.BeginForm MVC4通模式 [英] MVC4 Pass model via Ajax.BeginForm

查看:120
本文介绍了通过Ajax.BeginForm MVC4通模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图遵循一些好的帖子在这里得到这个工作,但每次我点击了ajax回发在我的控制器断点显示了空值的模型。只能查看该页面显示模式的值,但是我试图把他们在自己的形式,并在阿贾克斯的形式包装他们似乎没有任何工作。

I have tried to follow some good posts here to get this to work but every time i click the ajax postback the break point in my controller shows a model with null values. This page displays model values for viewing only, but I have tried putting them in their own form and also wrapping them in the ajax form and nothing seems to work.

@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>

的看法是很长(只显示模型值查看)在这里略。

The view is really long (just displays model values for viewing) abbreviated here.

<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>
}

控制器。我已经尝试了以上一种或两种形式。有了上面code模型包含了除这是在一个隐藏的输入字段酿提供ProfileID所有空值。我想通过该模型应该工作而无需创建一个隐藏字段为每个模型的价值?

Controller. I have tried the above in one or two forms. With the above code the model contains all empty values except the ProfileID which is stuffed in a hidden input field. I think passing the model should work without having to create a hidden field for every model value?

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

答案

该模型值不会因绑定到使用DisplayFor的

The model values are not binded due to the use of DisplayFor

推荐答案

由于在评论中指出的,实际上是希望看到你的模型已使用把你的表单项目 DisplayFor DisplayFor 字段的将不会发布后,他们只是用于显示。为了发表这些值回来了,你需要一个 HiddenFor 每个要发布,为了让模型绑定工作它的神奇领域。

As noted in the comments, you're actually expecting to see items in your Model that you have put in your form using DisplayFor. DisplayFor fields will not post, they are simply for display. In order to post those values back, you'd need a HiddenFor for each of the field that you want to post, in order for model binding to work it's magic.

这篇关于通过Ajax.BeginForm MVC4通模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆