[英]How to read a chosen value of HTML.RadioButtonFor in HTTPPOST?
I've read a lot of answers at SO, however it is not fit to me. 我已经在SO上阅读了很多答案,但是它不适合我。
I have model: 我有模特:
class SomeOrder
{
public string TypeDrink {get; set;}
}
Controller: 控制器:
public ViewResult Edit(int id)
{
SomeOrder se = newSomeOrder{ TypeDrink=3 };
return View(se);
}
And view: 并查看:
@Html.EditorForModel @Html.RadioButtonFor(m=>m.TypeDrink, "1") Tea
@Html.RadioButtonFor(m=>m.TypeDrink, "2") Coffee
@Html.RadioButtonFor(m=>m.TypeDrink, "3") Juice
How to read a chosen value of radiobutton in a [HTTPPOST] method? 如何在[HTTPPOST]方法中读取单选按钮的选定值? In my HTTPPOST method the preselected value is stored, not the chosen by user:
在我的HTTPPOST方法中,存储了预先选择的值,而不是用户选择的值:
[HTTPPOST]
public ViewResult Edit(SomeOrder se)
{
string chosenValue=se.TypeDrink;// always the old selected value
}
if your post action is like 如果您的张贴动作是
[HttPost]
public ViewResult Edit(SomeOrder model)
{
// should get it like
model.TypeDrink;
}
this is utilizing the model binding in mvc. 这是利用mvc中的模型绑定。
you could also look at the Request.Form["TypeDrink"]
to get the value, but it is not recomended 您还可以查看
Request.Form["TypeDrink"]
以获取该值,但不建议这样做
Your Model is: 您的模型是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace radiotest.Models
{
public class SomeOrder
{
public string TypeDrink { get; set; }
}
}
Your view is index.cshtml 您的视图是index.cshtml
@model radiotest.Models.SomeOrder
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
<fieldset>
<div class="editor-field">
@Html.RadioButtonFor(m => m.TypeDrink, "1") Tea
@Html.RadioButtonFor(m => m.TypeDrink, "2") Coffee
@Html.RadioButtonFor(m => m.TypeDrink, "3") Juice
</div>
<div> <input type="submit" value="Submit" /></div>
</fieldset>
}
Your Controller in HTTP Get and Post is: HTTP Get and Post中的控制器是:
using radiotest.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace radiotest.Controllers
{
public class TestController : Controller
{
public ActionResu`enter code here`lt Index()
{
SomeOrder se = new SomeOrder{ TypeDrink="3" };
return View(se);
}
[HttpPost]
public ActionResult Index(SomeOrder model)
{
//model.TypeDrink gives you selected radio button in HTTP POST
SomeOrder se = new SomeOrder {TypeDrink = model.TypeDrink };
return View(se);
}
}
}
Your view includes @Html.EditorForModel()
before your radio buttons. 您的视图在单选按钮之前包含
@Html.EditorForModel()
。 EditorForModel()
will generate form controls for each property in your model so it will be generating a control for property TypeDrink
. EditorForModel()
将为模型中的每个属性生成表单控件,因此它将为属性TypeDrink
生成控件。 Depending on attributes applied to your property, and any EditorTemplates
you may have, it may generated in a hidden input. 根据应用于属性的属性以及您可能拥有的任何
EditorTemplates
,它可能会在隐藏的输入中生成。
Because your form then posts back the name/pair values of the input generated by EditorForModel
first, the value of the input will be bound to your model and the value of the radio buttons will be ignored by the DefaultModelBinder
. 因为您的表单随后首先回发了由
EditorForModel
生成的输入的名称/对值,所以输入的值将绑定到您的模型,并且DefaultModelBinder
会忽略单选按钮的值。
Remove the EditorForModel
from your view and the model will be bound based on value of the radio buttons. 从视图中删除
EditorForModel
,然后将基于单选按钮的值绑定模型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.