我如何可以覆盖RadioButtonFor的name属性? [英] How can I override the name attribute of a RadioButtonFor?

查看:117
本文介绍了我如何可以覆盖RadioButtonFor的name属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一起组正在使用一个for循环和剃刀语法创建单选按钮。这里是code:

  @for(VAR I = 0; I< Model.Sessions.Count();我++)
       {
            @ Html.HiddenFor(IT = GT; it.Sessions [I] .ID)
            @ Html.RadioButtonFor(IT = GT; it.Sessions [I] .Checkbox,0,新{@class =会话,@id = ID,@ NAME =会话})
            @ Html.LabelFor(IT = GT; it.Sessions [I] .name和Model.Sessions [I] .Name点)
            &LT;跨度class=\"time-span\"><em>@Model.Sessions[i].StartTime</em><em>@Model.Sessions[i].EndTime</em></span>
            &LT; BR /&GT;
       }

里面的for循环的第三行是问题的所在。基本上名称不会改变,它总是会话[X] .Checkbox。该复选框是一个自定义类的属性(布尔)。我似乎无法获得调试剃刀东西的窍门,所以任何帮助将大大AP preciated,我猜这将是有人在这里极为明显。

修改
季米特洛夫的帖子帮了不少忙。下面是最终的code口使用。我用的是@class和@id属性可以使用Javascript功能来选择最初选择了会话(因为这是一个编辑,不能创建形式)。

  @for(VAR I = 0; I&LT; Model.Sessions.Count();我++)
    {
        @ Html.HiddenFor(IT = GT; it.Sessions [I] .ID)
        VAR SID = @ M​​odel.Sessions [I] .ID;
        @ Html.RadioButtonFor(IT = GT; it.selectedSession,Model.Sessions [I] .ID,新{ID = SID,@class =会话})
        @ Html.LabelFor(IT = GT; it.Sessions [I] .name和Model.Sessions [I] .Name点)
        &LT;跨度class=\"time-span\"><em>@Model.Sessions[i].StartTime</em><em>@Model.Sessions[i].EndTime</em></span>
        &LT; BR /&GT;
    }


解决方案

如果您希望能够只选择你需要对你的视图模型的单一属性来保存所选择的会话ID,这样一个单选按钮

 公共类SessionViewModel
{
    公众诠释SelectedSessionId {搞定;组; }
    公众的IList&LT;会议&GT;会议{搞定;组; }
}公共类会议
{
    公众诠释标识{搞定;组; }
    公共字符串名称{;组; }
}

,然后有一个控制器:

 公共类HomeController的:控制器
{
    公众的ActionResult指数()
    {
        VAR模型=新SessionViewModel
        {
            SelectedSessionId = 2,
            会话= Enumerable.Range(1,5)。选择(X =&gt;新建会话
            {
                ID = X,
                NAME =会话+ X,
            })。了ToList()
        };
        返回查看(模型);
    }    [HttpPost]
    公众的ActionResult指数(SessionViewModel模型)
    {
        返回的内容(感谢您选择会话ID:+ model.SelectedSessionId);
    }
}

和最后一个观点:

  @model SessionViewModel@using(Html.BeginForm())
{
    对于(VAR I = 0; I&LT; Model.Sessions.Count();我++)
    {
        @ Html.HiddenFor(X =&GT; x.Sessions [I] .ID)
        @ Html.RadioButtonFor(X =&GT; x.SelectedSessionId,Model.Sessions [I] .ID,新{ID =session_+ I})
        @ Html.Label(session_+ I,Model.Sessions [I] .Name点)
        &LT; BR /&GT;
    }
    &LT;按钮式=提交&GT;确定&LT; /按钮&GT;
}

I'm trying to group together radiobuttons that are creating using a for loop and Razor syntax. Here is the code:

       @for (var i = 0; i < Model.Sessions.Count(); i++)
       {
            @Html.HiddenFor(it => it.Sessions[i].Id)
            @Html.RadioButtonFor(it => it.Sessions[i].Checkbox, "0", new {@class = "Sessions", @id = id, @name="Sessions"})
            @Html.LabelFor(it => it.Sessions[i].Name, Model.Sessions[i].Name)
            <span class="time-span"><em>@Model.Sessions[i].StartTime</em><em>@Model.Sessions[i].EndTime</em></span>
            <br />
       }

The third line inside the for loop is where the problem is. Basically the name doesn't change and it's always "Sessions[x].Checkbox". The checkbox is a property (bool) of a custom class. I can't seem to get the hang of debugging Razor stuff, so any help would be greatly appreciated, I'm guessing this will be extremely obvious to someone here.

EDIT Dimitrov's post helped a lot. Below is the final code I used. I use the @class and @id attributes to be able to use Javascript to select the session originally picked (since this is an edit, not create form).

    @for (var i = 0; i < Model.Sessions.Count(); i++)
    {
        @Html.HiddenFor(it => it.Sessions[i].Id)
        var SId = @Model.Sessions[i].Id;
        @Html.RadioButtonFor(it => it.selectedSession, Model.Sessions[i].Id, new { id = SId, @class = "Sessions" })
        @Html.LabelFor(it => it.Sessions[i].Name, Model.Sessions[i].Name)
        <span class="time-span"><em>@Model.Sessions[i].StartTime</em><em>@Model.Sessions[i].EndTime</em></span>
        <br />
    }

解决方案

If you want to be able to select only a single radio button you need to have a single property on your view model to hold the selected session id, like this:

public class SessionViewModel
{
    public int SelectedSessionId { get; set; }
    public IList<Session> Sessions { get; set; }
}

public class Session
{
    public int Id { get; set; }
    public string Name { get; set; }
}

and then have a controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new SessionViewModel
        {
            SelectedSessionId = 2,
            Sessions = Enumerable.Range(1, 5).Select(x => new Session
            {
                Id = x,
                Name = "session" + x,
            }).ToList()
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(SessionViewModel model)
    {
        return Content("Thank you for selecting session id: " + model.SelectedSessionId);
    }
}

and finally a view:

@model SessionViewModel

@using (Html.BeginForm())
{
    for (var i = 0; i < Model.Sessions.Count(); i++)
    {
        @Html.HiddenFor(x => x.Sessions[i].Id)
        @Html.RadioButtonFor(x => x.SelectedSessionId, Model.Sessions[i].Id, new { id = "session_" + i })
        @Html.Label("session_" + i, Model.Sessions[i].Name)
        <br/>
    }
    <button type="submit">OK</button>
}

这篇关于我如何可以覆盖RadioButtonFor的name属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆