C#MVC如何将按钮值传回控制器?

时间:2017-01-26 07:55:06

标签: c# asp.net-mvc

我想在用户点击按钮时将按钮值恢复为控制器。我在网上搜索了几种方法,但都无法得到结果。任何人都可以帮忙吗?谢谢。

目前的结果是:

enter image description here

我想将按钮时间值替换为“时间结果...”

控制器:

public ActionResult Timeline()
{
    List<String> token = new List<string>();
    ViewBag.Date = "Time result......";

    using (SqlConnection con = new SqlConnection(constr))
    {
        string query = " SELECT TakenDate FROM ReferenceWound WHERE NRIC = '" + "S2686621B" + "' AND NurseInCharge = '" + User_Nurse + "'";
        using (SqlCommand cmd = new SqlCommand(query))
        {
            cmd.Connection = con;
            con.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    token.Add(sdr["TakenDate"].ToString());
                }
            }

            var datetaken = new List<PatientModel>();

            foreach(var date in token)
            {
                datetaken.Add(new PatientModel()
                {
                    Date = date
                });
            }
            con.Close();

            return View(datetaken);
        }
    }
}

型号:

public class PatientModel
{
    [Key]
    public int ID { get; set; }

    public string PatientNRIC { get; set; }

    public string NurseInCharge { get; set; }

    public List<SelectListItem> PatientList { get; set; }

    public string Date { get; set; }
}

查看:

@using (Html.BeginForm("Timeline", "DashBoard", FormMethod.Post))
{
    <table>
        <tr>
            @foreach (var dest in Model)
            {
                <td style="width: 100px; text-align: center;">@dest.Date</td>
            }
        </tr>
        <tr>
            @foreach (var dest in Model)
            {
                <td style="width: 100px; text-align: center;"><input type="submit" value="@dest.Date" /></td>                                                                                                                                                                                                                                                                        
            }
        </tr>
    </table>
}

@ViewBag.Date

3 个答案:

答案 0 :(得分:3)

简化您的示例

模型

public class Patient
{
    public DateTime date { get; set; }
}

控制器

public class HomeController : Controller
    {

        public ActionResult Index()
        {
            var p1 = new Patient { date = DateTime.Now };
            var p2 = new Patient { date = DateTime.Now.AddDays(1) };
            var listOfPatients = new List<Patient>();
            listOfPatients.Add(p1);
            listOfPatients.Add(p2);
            return View(listOfPatients);
        }

        public ActionResult Timeline(DateTime date)
        {
            return Content(date.ToString());
        }

    }

查看

@model IEnumerable<experiment.Models.Patient>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@using (Html.BeginForm("Timeline", "Home", FormMethod.Post))
{
    <fieldset>
        <legend>Patient</legend>
        @foreach(var p in Model)
        {
            <button type="submit" name="date" value="@p.date">@p.date</button>
        }
    </fieldset>
}

在ASP.NET MVC 4中测试

答案 1 :(得分:2)

为按钮添加名称属性:

<input type="submit" id="btnSubmit" name="btnSubmit" value="@dest.Date" />

创建一个操作方法来接受按钮的POST请求,传入视图的FormCollection:

public ActionResult Timeline(FormCollection form)
{
     var dest = form["btnSubmit"]; // access the value here
}

此方法允许您访问视图表单中的所有控件。

答案 2 :(得分:1)

您可以为按钮命名:

<input type="submit" name="myButton" value="@dest.Date" />

然后您的控制器操作可以将其作为参数:

[HttpPost]
public ActionResult Timeline(string myButton)
{
    ...
}