MVC中带有@ Html.DropDownListFor字段的可编辑HTML表

时间:2019-03-07 07:01:31

标签: c# html asp.net-mvc

我想在.net MVC中创建具有可编辑字段的HTML表格页面。我可以为TextBoxFor字段执行此操作。在页面提交时,Post控制器方法从表中接收值。使用DropDownListFor时遇到问题。页面使用DropDownListFor字段呈现,但是在提交后,后控制器方法接收Model对象,该对象的属性中没有任何数据。在下面的代码片段中,SaveMyItems控制器操作方法确实接收到具有设置为其属性的空值的Model对象。

这是我的模型课程

public class ItemsEntryModel
{
  public ItemModel Item { get; set; }
  public List<MyItemOptions> MyItemsOptionList { get; set; }  
  //individual field of HTML table      
}

public class ItemModel
{
  public int ID { get; set; }
  public string ItemName { get; set; }
}

public class MyItemOptions
{
  public int ID { get; set; }
  public int MyItemOptionUniqueNo { get; set; }
  public string OptionName { get; set; }
  public MyItemSubOptions SelectedItemSubOption { get; set; }
  public List<MyItemSubOptions> MyItemSubOptionsList { get; set; } 
  //bind in individual dropdown of field
}

public class MyItemSubOptions
{
  public int ID {get; set;}
  public string SubOptionName {get; set;}
}

调用Controlller操作方法的Java脚本代码以呈现部分视图并提交部分视图

$.ajax({
    url: '/MyController/MyItemsPartialView',
    contentType: 'application/html ; charset:utf-8',
    type: 'GET',
    data: {
        Id: data
    },
    dataType: 'html'
}).done(function (result) {
    //result of MyItemsPartialView action method is displayed in partial view
});

$.post('/MyController/SaveMyItems', $(this).serialize(), function (response) {
    
    //response of SaveMyItems action method is displayed on main view
});
@model ItemsEntryModel

<div class="row">
    <div class="form-group col-lg-12 mb-4">
        <div class="col-lg-12">
            @Model.Item.ItemName
        </div>
        <table class="table table-condensed table-hover bg-inverse-warning">
            <tr>
                @foreach (var item in Model.MyItemsOptionList)
                {
                    <td>
                        @Html.DisplayFor(modelItem => item.OptionName)
                    </td>
                }
            </tr>
            <tr>
                @foreach (var item in Model.MyItemsOptionList)
                {
                    <td>
                        @Html.DropDownListFor(modelItem => item.SelectedItemSubOption, new SelectList(item.MyItemSubOptionsList, "ID", "Name"), new { @id = "Product_" + item.MyItemOptionUniqueNo, @class = "form-control" })
                        <input id="lblMyItemOptionUniqueNo" type="hidden" value="@Html.DisplayFor(modelItem => item.MyItemOptionUniqueNo)" />
                    </td>
                }
            </tr>
        </table>
    </div>
</div>

0 个答案:

没有答案