【问题标题】:ASP.Net MVC: Selected country id value in dropdown not passing to controllerASP.Net MVC:下拉列表中选定的国家/地区 ID 值未传递给控制器
【发布时间】:2018-02-13 14:45:06
【问题描述】:

我有 webgrid,我在其中显示学生数据,如 ID、姓名和学生国家。所有数据都是可编辑的格式。国家/地区数据显示在下拉列表中。

我的用户界面

这是我的控制器代码

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

视图模型代码

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 从下拉菜单更改为未通过操作。 country id 也是 0。

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    您的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" }
        )
    )
    

    另外,将DropDownListFor()方法中的new { id = "cboCountry" }去掉,该方法会生成无效的html(重复的id属性)

    请注意,您当前的代码将每个&lt;select&gt; 中选定选项的值绑定到您的SelectedCountryId 属性,但DefaultModelBinder 仅绑定请求中的第一个值,因此SelectedCountryId 的值将是第一行中所选选项的值(即图片中与“印度”关联的 ID)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多