如何从mvc中的radiobuttonfor获取唯一ID
how to get unique id from radiobuttonfor in mvc
我有以下型号
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>
@Html.RadioButtonFor(modelItem => item.category_id, item.category_id, new { id=item.category_id})@item.category_desc
}
post方法
public ActionResult Category(category g)
{
}
在 post 中,方法 g
将以 null
的形式出现。
我在这里缺少什么?
你误解了单选按钮的工作原理。它们用于将多个选项之一绑定到单个 属性,与下拉列表的工作方式相同。您的代码正在生成绑定到自身的单选按钮。如果你检查 html 你会看到它生成 name="item.category_id"
无论如何都不会绑定到你的模型(它只会绑定到包含名为 Item
的复杂对象的模型,其中包含属性 名为 category_id
).
假设您有一个 Product
的模型,其中包含 Category
的 属性,并且您想要分配 category
table 到 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
可空 属性 的使用在
中进行了说明
然后控制器方法(对于创建视图)看起来像
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。
我有以下型号
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>
@Html.RadioButtonFor(modelItem => item.category_id, item.category_id, new { id=item.category_id})@item.category_desc
}
post方法
public ActionResult Category(category g)
{
}
在 post 中,方法 g
将以 null
的形式出现。
我在这里缺少什么?
你误解了单选按钮的工作原理。它们用于将多个选项之一绑定到单个 属性,与下拉列表的工作方式相同。您的代码正在生成绑定到自身的单选按钮。如果你检查 html 你会看到它生成 name="item.category_id"
无论如何都不会绑定到你的模型(它只会绑定到包含名为 Item
的复杂对象的模型,其中包含属性 名为 category_id
).
假设您有一个 Product
的模型,其中包含 Category
的 属性,并且您想要分配 category
table 到 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
可空 属性 的使用在
然后控制器方法(对于创建视图)看起来像
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。