单击按钮后我的列表变量数组丢失

时间:2018-05-01 20:25:56

标签: asp.net-mvc

我没有DB,而不是DB我必须列出数组,我有一个表,当我想要编辑Driver的详细信息时,我点击'edit',之后显示更改为带有详细信息的页面,之后我可以更改详细信息并单击保存按钮,我的问题是单击按钮'保存'后,我的阵列列表未命中,我在MVC中不是很好,但我尽我所能!如果有人看到我做错了,请告诉我并解释一下,谢谢

我的控制器

$xml -match $oldString

我的View Edit,当我点击按钮时,我的参数没有保存在我的列表中,我不明白为什么

public class DriverTaxiController : Controller
{

    static  List<Drivers> Driver = new List<Drivers>();
    public static  int numLine = -1;

    // GET: DriverTaxi
    public ActionResult List()
    {

        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)
    {
       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;
                ViewBag.line = Driver[i].Line;
            }
        }

     numLine = id;

    return View();
}

[HttpPost]
public ActionResult Update()
{

    if (ModelState.IsValid)
    {
        numLine--;
        Driver[numLine].NumberLicens = Request.Form["NumberLicens"];
        Driver[numLine].FirstName = Request.Form["FirstName"];
        Driver[numLine].LastName = Request.Form["LastName"];
        Driver[numLine].PhoneNumber = Request.Form["PhoneNumber"];
        Driver[numLine].StartWork = Request.Form["StartWork"];
        Driver[numLine].DateCheckEyes = Request.Form["DateCheckEyes"];

        return View("List2");
    }
    else
    {
        return View("MyAction");
    }

}

那是我的表

     @using (Html.BeginForm("Update", "DriverTaxi"))
     {
        @Html.TextBoxFor(Model => Model.NumberLicens, new { @Value = @ViewBag.nl }) @Html.ValidationMessageFor(x => x.NumberLicens)
        <br />
        @Html.TextBoxFor(Model => Model.FirstName, new { @Value = @ViewBag.fn })
        <br />
        @Html.TextBoxFor(Model => Model.LastName, new { @Value = @ViewBag.ln })
        <br />
        @Html.TextBoxFor(Model => Model.PhoneNumber, new { @Value = @ViewBag.phone })
        <br />
        @Html.TextBoxFor(Model => Model.StartWork, new { @Value = @ViewBag.start })
        <br />
        @Html.TextBoxFor(Model => Model.DateCheckEyes, new { @Value = @ViewBag.eye })
        <br />

        @Html.HiddenFor(Model => Model.Line)

        <input type="submit" value="Save" />
        @Html.ValidationSummary()
    }

那是我的错误 enter image description here

1 个答案:

答案 0 :(得分:0)

只需将ViewBag分配添加到呈现视图的每个操作中。像这样:

public ActionResult MyAction(int  id=0)
{
   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;
            ViewBag.line = Driver[i].Line;
        }
    }

    numLine = id;
    ViewBag.Drivers = Driver; //TODO
    return View();
}

[HttpPost]
public ActionResult Update()
{

    if (ModelState.IsValid)
    {
        numLine--;
        Driver[numLine].NumberLicens = Request.Form["NumberLicens"];
        Driver[numLine].FirstName = Request.Form["FirstName"];
        Driver[numLine].LastName = Request.Form["LastName"];
        Driver[numLine].PhoneNumber = Request.Form["PhoneNumber"];
        Driver[numLine].StartWork = Request.Form["StartWork"];
        Driver[numLine].DateCheckEyes = Request.Form["DateCheckEyes"];

        ViewBag.Drivers = Driver; //TODO

        return View("List2");
    }
    else
    {
        ViewBag.Drivers = Driver; //TODO

        return View("MyAction");
    }

}