[英]how to get unique id from radiobuttonfor in mvc
I have a below model 我有一个下面的模型
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; }
}
controller passing the model to view 控制器将模型传递给视图
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);
}
In view populating the radion button 在视图中填充单选按钮
@foreach (var item in Model)
{
@Html.DisplayFor(modelItem => item.display_sno)<text>.</text>
@Html.RadioButtonFor(modelItem => item.category_id, item.category_id, new { id=item.category_id})@item.category_desc
}
post Method 发布方法
public ActionResult Category(category g)
{
}
In post method g
is coming as null
. 在post方法中,
g
为null
。 What I am missing here? 我在这里想念的是什么?
Your misunderstanding how radio buttons work. 您对单选按钮如何工作的误解。 They are for binding one of many options to a single property, in the same way a dropdownlist works.
它们用于将多个选项之一绑定到单个属性,就像下拉列表一样。 Your code is generating radio buttons that bind to itself.
您的代码正在生成绑定到自身的单选按钮。 And if you inspect the html you will see that it generates
name="item.category_id"
which would not bind to your model anyway (that would only bind to a model containing a complex object named Item
which contained a property named category_id
). 并且,如果您检查html,您将看到它生成的
name="item.category_id"
不会绑定到您的模型(只会绑定到包含名为Item
的复杂对象的模型,该对象包含一个名为category_id
的属性)。
Assuming you have a model for a Product
which contained a property for its Category
, and you wanted to assign one of the values of your category
table to the Category
property, then your view models would look like 假设你有一个模型
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; }
}
Note the use of a nullable property for Category
is explained in What does it mean for a property to be [Required] and nullable? 请注意,
Category
为[必填]和可为空意味着什么,说明了对Category
使用可为空的属性?
Then the controller methods (for a Create view) would look like 然后,控制器方法(用于“创建”视图)看起来像
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
});
}
Then the view would be 那么视图将是
@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" .... />
}
Note that the new { id = "" }
is removing the id
attribute which would otherwise be invalid html because of duplicates. 请注意,
new { id = "" }
会删除id
属性,否则该属性将由于重复而无效html。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.