【问题标题】:how to get unique id from radiobuttonfor in mvc如何从mvc中的radiobuttonfor获取唯一ID
【发布时间】:2017-11-16 05:21:14
【问题描述】:

我有一个下面的模型

public class category
{
    [Key]
    public long category_id { get; set; }
    public string category_desc { get; set; }
    public long? client_id { get; set; }
    public long? display_sno { get; set; }
}

控制器将模型传递给视图

public ActionResult category(long? client_id)
{
    var category_type = db.category.Where(m => m.client_id == null).ToList();
    if(client_id == 10)
    {
        category_type = db.category.Where(m => m.client_id == client_id).ToList();
    }
    return View(category_type);
}

在视图中填充单选按钮

@foreach (var item in Model)                                                 
{ 
    @Html.DisplayFor(modelItem => item.display_sno)<text>.</text> &nbsp;
    @Html.RadioButtonFor(modelItem => item.category_id, item.category_id, new { id=item.category_id})@item.category_desc
}

发布方法

public ActionResult Category(category g)
{

}

在 post 方法中 gnull。 我在这里缺少什么?

【问题讨论】:

  • 您的单选按钮没有意义。您需要一组单选按钮绑定到具有不同值的相同属性。不清楚你想用这段代码做什么
  • @StephenMuecke category_id 对于每个单选按钮都是唯一的,并且在回发时需要获取选定的 category_id
  • 是的,但是为此您需要一个具有属性的模型(比如说public long category_id { get; set; })和另一个属性,它是一个包含每个单选按钮值的集合(比如说public IEnumerable&lt;int&gt; Values { get; set; },然后是它foreach (var item in Model.Values { @Html.RadioButtonFor(m =&gt; m.category_id, item) }
  • 假设Values[1,2,3,4] 将生成&lt;input type="radio" name="category_id" value="1"&gt;(和value="2" 等)。当您提交时,如果您选择了第二个单选按钮,category_id 的值将是 2
  • @StephenMuecke 值不是 1,2,3... category_id 是随机数..

标签: asp.net-mvc asp.net-mvc-4 radiobuttonfor


【解决方案1】:

您对单选按钮工作原理的误解。它们用于将多个选项之一绑定到单个属性,与下拉列表的工作方式相同。您的代码正在生成绑定到自身的单选按钮。如果您检查 html,您将看到它生成 name="item.category_id" 无论如何都不会绑定到您的模型(只会绑定到包含名为 Item 的复杂对象的模型,该对象包含名为 category_id 的属性)。

假设您有一个Product 的模型,其中包含其Category 的属性,并且您想将category 表的值之一分配给Category 属性,那么您的视图模型将看起来像

public class ProductVM
{
    [Required(ErrorMessage = "Please select a category")]
    public int? Category { get; set; }
    public IEnumerable<CategoryVM> CategoryOptions { get; set; }
}

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

注意Category 的可为空属性的使用在What does it mean for a property to be [Required] and nullable? 中进行了说明

然后控制器方法(用于创建视图)看起来像

public ActionResult Create()
{
    ProductVM model = new ProductVM();
    ConfigureViewModel(model);
    return View(model);
}
public ActionResult Create(ProductVM model)
{
    if (!ModelState.IsValid())
    {
        ConfigureViewModel(model);
        return View(model);
    }
    .... // Initialize your Product data model, set its properties based on the view model
    // Save and redirect
}
private void ConfigureViewModel(ProductVM model)
{
    model.CategoryOptions = db.categories.Select(x => new CategoryVM
    {
        ID = x.category_id,
        Name = x.category_desc
    });
}

那么视图将是

@model ProductVM
....
@using (Html.BeginForm())
{
    @Html.DisplayNameFor(m => m.Category)
    foreach(var category in Model.CategoryOptions)
    {
        <label>
            @Html.RadioButtonFor(m => m.Category, category.ID, new { id = "" })
            <span>@category.Name</span>
        </label>
    }
    @Html.ValidationMessageFor(m => m.Category)
    <input type="submit" .... />
}

请注意,new { id = "" } 正在删除 id 属性,否则该属性会因为重复而成为无效的 html。

【讨论】:

    猜你喜欢
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-10
    相关资源
    最近更新 更多