检索ViewModel数据集合为空

时间:2015-04-13 07:42:42

标签: c# ajax asp.net-mvc

我将模型从视图传递到控制器。在我的viewmodel List<TradeLaneDetailsDTO>总是为null,我通过ajax传递我的数据。我的代码中有什么问题。 请帮帮我......

这是我的viewmodel

public class SLAViewModel 
    {
        public List<TradeLaneDetailsDTO> Items { get; set; }
    }

这是我的观点

@using (Html.BeginForm("SaveSLA", "SLAMgmt", FormMethod.Post, htmlAttributes: new { @class = "form-horizontal", @role = "form", id = "frmEstDays" }))
                {

              for (int i = 0; i < Model.Items.Count; i++)
              {
                        <div class="form-group">
                            @Html.LabelFor(model => model.Items.ElementAt(i).legname, Model.Items.ElementAt(i).legname, new { @class = "col-md-4" })               
                            <div class="col-md-3">
                                @Html.TextBoxFor(model => model.Items.ElementAt(i).estddays, new { @class = "form-control", type = "text", MaxLength = "10" })     
                            </div>
                       </div>

              }

                <div class="form-group">
                    <div class="offset-3 col-md-8">
                        <button id="btnSave" type="button" title="Save" class="btn btn-success" onclick="getPage1('@Url.Action("SaveSLA", "SLAMgmt")')">
                        <span class="glyphicon glyphicon-floppy-disk"></span>Save
                        </button>
                    </div>
                </div>  
            }

这是我的ajax函数

function getPage1(page)
        {
            alert("get page1");
            $.ajax({
                type: "POST",
                url: page,
                data: $("#frmEstDays").serialize(),
                xhrFields: {
                    withCredentials: true
                },
                success: function (html) {
                    alert(html.responseText);
                },
                error: function (data) {
                    var error = "Error ";
                }
            });
        }

这是我的控制器功能

 public ActionResult SaveSLA(SLAViewModel slavModel)
            {
                string[] ErrorMessageArray = new string[4];
                int errorIndex = 0;
                 return anything;
            }

2 个答案:

答案 0 :(得分:0)

请从type = "text" html帮助中删除TextBoxFor

 @Html.TextBoxFor(model => model.Items.ElementAt(i).estddays, new { @class = "form-control", MaxLength = "10" }) 

HtmlTextboxFor始终会创建一个文本框<input type="text" />。所以你不必明确指定。

答案 1 :(得分:0)

只需使用model =&gt; model.Items [i] .PROPERTYNAME如下

 @Html.TextBoxFor(model => model.Items[i].estddays, new { @class = "form-control", type = "text", MaxLength = "10" })  

希望这会有所帮助