我如何判断单选按钮是否已选中? (MVC)

发布于 2024-09-25 21:40:17 字数 172 浏览 1 评论 0原文

我真的很希望能够做到这一点,

if myRB.Checked = true then
    return redirecttorout("SomeRoute")
else
    someother route

我习惯了网络表单,我可以在后面的代码中做到这一点

I would really like to be able to do this

if myRB.Checked = true then
    return redirecttorout("SomeRoute")
else
    someother route

i am used to web forms, in which i could do this in the code behind

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋风の叶未落 2024-10-02 21:40:17

Mvc 与普通 html post 一样,通过输入的名称而不是 id 返回值。在单选按钮集合中,所有单选按钮都具有相同的名称。所以你得到的是单选按钮的名称和所选单选按钮的值

代码最终看起来像

if(model.radio=="va1")
{
....
}

Mvc like normal html post returns values by an input's name not id. In a radio button collection all radio buttons have the same name. So what you get back is the name of the radio button with the value of the selected radio button

The code ends up looking like

if(model.radio=="va1")
{
....
}

最美的太阳 2024-10-02 21:40:17

在表单发布到的操作中,您需要创建一个字符串 var 来保存该值。

public ActionResult GetRadioButtonValue(string RadioButtonVal)
{

单选按钮的名称都需要在视图上匹配,但值将是发送到操作的值。确保上面看到的字符串与单选按钮的名称匹配。检查字符串是否为 null 或为空也是一个好主意。

string.IsNullOrEmpty(RadioButtonVal)

In the action that the form posts to you need to create a string var that will hold the value.

public ActionResult GetRadioButtonValue(string RadioButtonVal)
{

The Name of the radio buttons all need to match on the view, but the value will be what is sent to the Action. Ensure that the string as seen above matches the Name of the radio buttons. It would also be a good idea to check that string for is null or empty.

string.IsNullOrEmpty(RadioButtonVal)
梦魇绽荼蘼 2024-10-02 21:40:17

查看

@model MvcApplication2.Models.CountryModel
@using (Html.BeginForm())
{
            @Html.RadioButton("Gender", "Male" new { @checked = "checked" }) Male
            @Html.RadioButton("Gender", "Female")Female
            <input type="submit" text="submit"/>
}

型号

   public class CountryModel
    {
        public string Gender{ get; set; }

    }

控制器

public ActionResult Index()
{                    
    return View();
}
[HttpPost]
public ActionResult Index(CountryModel model)
{
    //for radio button
    CountryModel dd = new CountryModel();
    string date1 = model.RDB;
    return View(model);
}

View

@model MvcApplication2.Models.CountryModel
@using (Html.BeginForm())
{
            @Html.RadioButton("Gender", "Male" new { @checked = "checked" }) Male
            @Html.RadioButton("Gender", "Female")Female
            <input type="submit" text="submit"/>
}

MODEL

   public class CountryModel
    {
        public string Gender{ get; set; }

    }

CONTROLLER

public ActionResult Index()
{                    
    return View();
}
[HttpPost]
public ActionResult Index(CountryModel model)
{
    //for radio button
    CountryModel dd = new CountryModel();
    string date1 = model.RDB;
    return View(model);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文