0

I am getting the Form data in json string Format but unable to bind it with Model Object

My View:

           <div class="GenderaMale float_lt">
            </div>
            <div class="float_lt">@Html.RadioButtonFor(m => m.PersonalDetails.BasicDetails.Gender, "male", new { @style = "margin:10px;margin-top:15px;", id = "malegender" })
            </div>
            <div class="GenderaFemale float_lt" style="margin-left: 30px;">
            </div>
            <div class="float_lt">@Html.RadioButtonFor(m => m.PersonalDetails.BasicDetails.Gender, "Female", new { @style = "margin:10px;margin-top:15px;", id = "femalegender" })
            </div>
            <span class="float_lt" style="margin-right: 15px;
            margin-top: 5px;">Amount</span>
            <div class="rupee" style="float: left">
            </div>
            @Html.TextBoxFor(m => m.Amount, new { @class = "trackwidthtxt float_lt" })

<script type="text/javascript">

        $.fn.serializeObject = function () {
            var o = {};
            var a = this.serializeArray();
            $.each(a, function () {
                if (o[this.name] !== undefined) {
                    if (!o[this.name].push) {
                        o[this.name] = [o[this.name]];
                    }
                    o[this.name].push(this.value || '');
                } else {
                    o[this.name] = this.value || '';
                }
            });
            return o;
        };

        $("#calculateNow").click(function () {

            var viewmodel = $('#CalculateNowForm').serializeObject();
            alert(viewmodel);

            var data = JSON.stringify($('form').serializeObject())
            alert(data);
            var Parameters = '{"leadViewModel":' + data + '}';
            alert(Parameters);
            Events.OpenPopup(ApplicationRoot + '/Calculator/CalculateNow', '', 600, 750, "middle", "#E7F0F5", Parameters);

        });

    </script>

Here is My Model:

public partial class LeadViewModel 
{
    public string Amount{ get; set; }
    public PersonalDetailsViewModel PersonalDetails{ get; set; }
}

public partial class PersonalDetailsViewModel 
{
    public BasicDetailsViewModel BasicDetails{ get; set; }
}
public partial class BasicDetailsViewModel 
{
    public string Gender{ get; set; }
}

Here is my Controller:

[HttpPost]
        public ActionResult CalculateNow(LeadViewModel leadViewModel)
        {
            LeadViewModel leadmodel = leadViewModel;
            return PartialView("ConfirmToCalculate",leadmodel);
        }

Kindly go through my Jquery code attempt..I am getting null value in controller Method parameter i.e. on leadViewModel..Please help me..!!!

4

1 回答 1

0

只需传入序列化数据而不创建Parameters. 模型绑定会将表单值映射到LeadViewModel控制器方法的对象。

于 2013-05-02T08:13:32.497 回答