如何从View到Controller变量?

时间:2018-04-30 07:52:58

标签: asp.net-mvc asp.net-mvc-5

我需要从按钮到Controller获取变量。我有一个按钮'编辑'当我点击它时,我想将线路的编号发送到控制器,以便了解驱动程序需要编辑的内容。

这是我的控制器:

 static  List<Drivers> Driver = new List<Drivers>();

    // GET: DriverTaxi
    public ActionResult List()
    {

        if (Driver.Count == 0)
        {
            Driver.Add(new Drivers() { Line = 1, NumberLicens = "123456", FirstName = "Evgeny", LastName = "Ryvkin", PhoneNumber = "0546819725", StartWork = "12/10/17", DateCheckEyes = "13/10/17" });
            Driver.Add(new Drivers() { Line = 2, NumberLicens = "123457", FirstName = "Moshe", LastName = "Kohen", PhoneNumber = "0546819725", StartWork = "12/10/17", DateCheckEyes = "13/10/17" });
            Driver.Add(new Drivers() { Line = 3, NumberLicens = "123458", FirstName = "Dana", LastName = "Multy", PhoneNumber = "0546819725", StartWork = "12/10/17", DateCheckEyes = "13/10/17" });
        }

        ViewBag.Drivers = Driver;
        return View();
    }


    public ActionResult MyAction(int  id=0)
    {


       // int numberLine = Convert.ToInt32(lineDriver);//convert string to int
       for(int i = 0; i < Driver.Count; i++)
        {
            if(Driver[i].Line == id)
            {
                ViewBag.nl = Driver[i].NumberLicens;
                ViewBag.fn = Driver[i].FirstName;
                ViewBag.ln = Driver[i].LastName;
                ViewBag.phone = Driver[i].PhoneNumber;
                ViewBag.start = Driver[i].StartWork;
                ViewBag.eye = Driver[i].DateCheckEyes;
            }
        }

        return View();
    }

该表发送变量,但我无法在Controller中接收;我不明白为什么。

这是我的观点:

        <tr>
            <th>No.</th>
            <th>Number Licens</th>
            <th>Full Name</th>
            <th>Phone Number</th>
            <th>Start Work</th>
            <th>Date Cheking the Eyes</th>
            <th>Edit</th>
            <th>Delete</th>
        </tr>

        @foreach (Drivers p in ViewBag.Drivers)
    {
        <tr>
            <td>@p.Line</td>
            <td>@p.NumberLicens</td>
            <td>@p.FirstName &nbsp; @p.LastName </td>
            <td>@p.PhoneNumber</td>
            <td>@p.StartWork</td>
            <td>@p.DateCheckEyes</td>

            @using (Html.BeginForm("MyAction", "DriverTaxi"))
            {
            <td>
                <input type="hidden" name="DriverName" value="hello" />
                <input type="submit" name="123" value="Edit" />
                @Html.ActionLink("MyAction", "DriverTaxi", new { id = p.Line })
            </td>
            }
   }

谢谢!

1 个答案:

答案 0 :(得分:0)

This link会提供您所寻求的信息。

[编辑:没有看到视图代码。]
不要那样遍历你的列表。如果您拥有所寻找元素的索引,则可以使用Driver.ElementAt(i)。这将返回给定索引处的值。
此外,在您的视图中使用模型也许不是一个坏主意。这将使你对所有那些viewBags的需求模糊不清。

@model List<Drivers>

调用您的视图时,您可以将列表作为参数

return View(Driver);

然后当你遍历你的驱动程序时,你可以这样做:

foreach (var item in Model)
{
   <td>@item.Line</td>
   <td>@item.NumberLicens</td>
   // ...
}

至于你的ActionLink问题,其语法如下:

Html.ActionLink(article.Title, 
                "Item",   // <-- ActionMethod
                "Login",  // <-- Controller Name.
                new { article.ArticleID }, // <-- Route arguments.
                null  // <-- htmlArguments .. which are none. You need this value
                      //     otherwise you call the WRONG method ...
                      //     (refer to comments, below).
                )

这意味着你有了#34; MyAction&#34;在印刷价值所在的地方,而不是你的行动方法,也许这会有效:

 @Html.ActionLink("Edit","MyAction", "DriverTaxi", new { id = p.Line },null)

我不是专家,所以如果我的信息有误,请随时纠正。