取ID和值从邮寄大量的单选框 [英] taking id and value from a lot of radiobutton by post
问题描述
在认为我有很多的单选按钮
和id是唯一的每一个。当用户选择那些在控制器的一个,我想取 ID
和值
我怎么能做到这一点.. ..我尝试用的FormCollection
但在这里我只是可以采取的价值...
In the view I have a lot of RadioButtons
and the id is the unique for each one. when user choose one of those in controller I would like to take the ID
and VALUE
how i can do that.... i try with formCollection
but in here i just can take value...
@Html.RadioButton("BookType", "1", new { id = "100" })
<br>
Above code generates Below code
<br/>
< input id="100" name="BookType" type="radio" value="1" >
和问题是我怎么可以通过控制POST
操作中的ID和价值。
and the question is how can I take the ID and VALUE by 'POST'
Action in the control.
推荐答案
我建议你使用的视图模型。请波纹管看到这样的例子:
I suggest you to use view model. Please see this example bellow:
namespace MvcApplication1.Controllers {
public class TheOption {
public string Id { get; set; }
public string Value { get; set; }
public bool? Checked { get; set; }
}
public class FooController : Controller {
//
// GET: /Foo/
public ActionResult Index() {
var options = new List<TheOption> {
new TheOption {Id = "One", Value = "The One"},
new TheOption {Id = "Two", Value = "The Two"},
new TheOption {Id = "Hundred", Value = "The Hundred"},
};
return View(options);
}
[HttpPost]
public ActionResult Index(List<TheOption> options) {
return View(options);
}
}
}
现在你需要创建 TheOption
模型编辑器模板。它只是由下创建名为〜 EditorTemplates \\浏览\\共享\\文件夹中的文件夹。添加新视图编辑器模板。命名此编辑模板,以配合型号名称( TheOption
)。
Now you need to create editor template for TheOption
model. It's simply by create folder named EditorTemplates under ~\Views\Shared\ folder. Add new view as the editor template. Name this editor template to match with the model name (TheOption
).
下面是内容的〜\\查看\\共享\\ EditorTemplates \\ TheOption.cshtml
@model MvcApplication1.Controllers.TheOption
<div>
@Html.RadioButtonFor(m => m.Checked, true, new { id = Model.Id + "_selected" })
@Html.RadioButtonFor(m => m.Checked, false, new { id = Model.Id }) @Model.Value
@Html.HiddenFor(m => m.Id)
@Html.HiddenFor(m => m.Value)
</div>
现在去你的主视图(Index.cshtml),简单地把这个code:
Now go to your main view (Index.cshtml) and simply put this code:
@model System.Collections.Generic.List<MvcApplication1.Controllers.TheOption>
@using (Html.BeginForm()) {
@Html.EditorFor(m=>m)
<button type="submit">Save</button>
}
完成!希望这有助于:)
Done! Hope this helps :)
这篇关于取ID和值从邮寄大量的单选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!