如何使用单选按钮设置文本框的值?

时间:2017-04-06 17:26:11

标签: c# asp.net-mvc asp.net-mvc-4 razor visual-studio-2012

这是我的控制器视图

public ActionResult rb()
    {
        subject ts = new subject();
        ts.subjectlist = db.tbSubjs.ToList();
        ts.selectsubj = "";
        return View(ts);
    }
    [HttpPost]
    public ActionResult rb(subject sj, FormCollection frm)
    {
        if (ModelState.IsValid)
        {
            var selectsubj = sj.selectsubj;
            string getcode = frm["subj"];
            ViewBag.ssss = getcode;
        }
        return View("Index");
    }

这是我的班级

public class subject
{
    public List<tbSubj> subjectlist { get; set; }
    public string selectsubj { get; set; }
}

这是我的单选按钮代码

<div style="text-align: left">
@using (Html.BeginForm("rb", "Home"))
{ 
<div>------</div>
foreach (var ts in Model.subjectlist)
{ 
<div>
    @Html.Label(ts.SubjCode)
    @Html.RadioButtonFor(m => m.subjectlist, ts.SubjCode)
</div>
}

foreach (var getsubj in Model.subjectlist)
{ 
    <div>------------------</div>
<div>
    @getsubj.SubjCode
    @Html.RadioButton("subj", getsubj.SubjCode)
</div>
}
<br />
<input id="Submit" type="submit" value="Submit" />
}
</div>

我希望通过获取单选按钮的值在此视图中的文本框中设置一个值。

@model ATM.Models.subject

@{
ViewBag.Title = "rb";
}

<h2>rb</h2>

@Html.TextBox(m => m.selectsubj)

如果有人愿意帮助我,请帮助我。

1 个答案:

答案 0 :(得分:0)

假设您的操作实际上看起来更像以下内容:

[HttpPost]
public ActionResult rb(subject sj, FormCollection frm)
{
    if (ModelState.IsValid)
    {
        var selectsubj = sj.selectsubj;
        string getcode = frm["subj"];
        ViewBag.ssss = getcode;

        return View("view_with_textbox");
    }

    return View("Index");
}

你可以这样做:

[HttpPost]
public ActionResult rb(subject sj, FormCollection frm)
{
    if (ModelState.IsValid)
    {
        var selectsubj = sj.selectsubj;
        string getcode = frm["subj"];

        var model = new ATM.Models.subject()
        {
            selectsubj = getcode
        };

        return View("view_with_textbox", model);
    }

    return View("Index");
}

从而通过模型传递值。