ASP.Net MVC:下拉列表中选定的国家/地区ID值未传递给控制器

时间:2018-02-13 14:45:07

标签: asp.net-mvc

我有webgrid,我在这里显示ID,姓名和学生国家等学生数据。所有数据均采用可编辑格式。国家/地区数据显示在下拉列表中。

我的用户界面

enter image description here

这是我的控制器代码

public class WebGridMoreControlsController : Controller
{
    // GET: WebGridMoreControls
    public ActionResult Index()
    {
        StudentListViewModel osvm = new StudentListViewModel();
        return View(osvm);
    }

    [HttpPost]
    public ActionResult Index(StudentListViewModel oStudentListViewModel)
    {
        return View(oStudentListViewModel);
    }
}

这是我的模型代码

public class Student
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int CountryID { get; set; }
}

public class Country
{
    public int ID { get; set; }
    public string Name { get; set; }
} 

viewmodel code

public class StudentListViewModel
{
    public IList<Student> Students { get; set; }

    public int SelectedCountryId { set; get; }
    public List<Country> Country { get; set; }

    public StudentListViewModel()
    {
        Students = new List<Student>
        {
            new Student{ID=1,Name="Keith",CountryID=1},
            new Student{ID=2,Name="Paul",CountryID=2},
            new Student{ID=3,Name="Sam",CountryID=3}
        };

        Country = new List<Country>
        {
            new Country{ID=1,Name="India"},
            new Country{ID=2,Name="UK"},
            new Country{ID=3,Name="USA"}


        };
    }
}

这是我的剃刀视图代码

@model MVCCRUDPageList.Models.StudentListViewModel
@{
    ViewBag.Title = "Index";
}

<h2>Student View Model</h2>

@using (Html.BeginForm("Index", "WebGridMoreControls", FormMethod.Post))
{
    var grid = new WebGrid(Model.Students, canSort: false, canPage: false);
    var rowNum = 0;


    <div id="gridContent" style=" padding:20px; ">
        @grid.GetHtml(
        tableStyle: "table",
        alternatingRowStyle: "alternate",
        selectedRowStyle: "selected",
        headerStyle: "header",
        columns: grid.Columns
        (
            grid.Column(null, null, format: item => rowNum = rowNum + 1),
            grid.Column("ID", format: (item) => @Html.TextBoxFor(m => m.Students[rowNum - 1].ID, new { @class = "edit-mode" })),
            grid.Column("Name", format: (item) => @Html.TextBoxFor(m => m.Students[rowNum - 1].Name, new { @class = "edit-mode" })),

             grid.Column("Country", format: (item) => 
                 @Html.DropDownListFor(x => x.SelectedCountryId,
                  new SelectList(Model.Country, "ID", "Name", Model.SelectedCountryId = item.CountryID), 
                 "-- Select States--", new { id = "cboCountry", @class = "edit-mode" }))
        ))
        <input type="submit" value="Submit" />
    </div>

}

我在哪里犯了错误,当我提交表格时,改变了国家ID从drowdown没有通过行动。 country id也是0。

1 个答案:

答案 0 :(得分:0)

您的Student模型包含属性CountryID,这是您需要绑定的属性。从视图模型中删除public int SelectedCountryId { set; get; }属性,并更改视图中的代码以绑定到CountryID

grid.Column("Country", format: (item) => 
    @Html.DropDownListFor(
        x => x.Students[rowNum - 1].CountryID, 
        new SelectList(Model.Country, "ID", "Name", item.CountryID), 
        "-- Select States--",
        new { @class = "edit-mode" }
    )
)

此外,从生成无效html(重复new { id = "cboCountry" }属性)的DropDownListFor()方法中删除id

请注意,您当前的代码会将每个<select>中所选选项的值绑定到您的SelectedCountryId属性,但DefaultModelBinder仅绑定请求中的第一个值,因此值为SelectedCountryId将是第一行中所选选项的值(即与图片中的&#39; India&#39;相关联的ID)。