数据丢失后POST ASP.Net MVC 4

本文关键字:Net MVC ASP POST 数据 | 更新日期: 2023-09-27 18:04:29

我一直在表单提交工作,我遇到了一些我不能弄清楚的东西。我知道这个问题已经发布了很多次,但我似乎无法解决。

模型:

public class IndexModel
{        
    public string techNo { get; set; }      
    public string firstName { get; set; }
    public string lastName { get; set; }
}

控制器:

public ActionResult Index(IndexModel _oTechModel)
{   
    //some codes on pulling data from the database
    return View("Index", _oTechModel);       
}
[HttpPost]
public ActionResult ProcessTechnician(FormCollection form, string SubmitButton)
{         
    IndexModel _oTechModel = new IndexModel();
    switch (SubmitButton)
    {
        case "save": //add new technician
            {                
            }
            break;
        case "update": //update technician details
            {
                try
                {
                    if (isValid(form["techNo"].ToUpper(), "technician") == false) { /*do nothing*/}
                    else
                    {
                        _GTechnician.TECHNICIAN_NO = form["techNo"].ToUpper();
                        _GTechnician.FIRSTNAME = form["lastName"].ToUpper(); //form is empty here
                        _GTechnician.LASTNAME = form["firstName"].ToUpper(); //form is empty here
                        _GTechnicianFacade.updateTechnician(_GTechnician, _GAppSetting.ConnectionString);
                    }
                }
                catch (Exception ex) { throw ex; }
            }
            break;
        case "search": //search technician
            {
                try
                {
                    if (isValid(form["techNo"].ToUpper(), "technician") == false) { /*do nothing*/}
                    else
                    {
                        //some codes here
                        _oTechModel.techNo = form["techNo"].ToUpper();
                        _oTechModel.firstName = fNameStr;
                        _oTechModel.lastName = lNameStr;
                        _oTechModel.setEnable = true;
                    }
                }
                catch (Exception ex) { throw ex; }
            }
            break;
        case "delete": 
            {                
            }
            break;
    }
    return RedirectToAction("Index", _oTechModel);
}

视图:

@model Maintenance_.Models.IndexModel
@using (Html.BeginForm("ProcessTechnician", "Home", FormMethod.Post))
{
    <td>Technician No :</td>
    @Html.TextBoxFor(m => m.techNo)
    <button type="submit" name="SubmitButton" value="search"></button>
    <td>First Name :</td>
    @Html.TextBoxFor(m => m.firstName, new { @class = "form-control", style = "width:380px;" })
    <td>Last Name :</td>
    @Html.TextBoxFor(m => m.lastName, new { @class = "form-control", style = "width:380px;" })
    <button name="SubmitButton" value="delete" type="submit">Delete</button>    
    <button name="SubmitButton" value="update" type="submit">Update</button>
    <button name="SubmitButton" value="save" type="submit">Save</button>
}

当我点击搜索按钮时,它提交表单并在文本框上显示技术人员的姓和名,但是当我点击更新按钮后更改文本框的值时,它清除文本框,数据丢失。我做错了什么?

数据丢失后POST ASP.Net MVC 4

post方法中的第一行是

IndexModel _oTechModel = new IndexModel();

然后用这个"空"模型重定向到索引页。(在Update语句的情况下,您没有为_otechModel分配任何值)

你需要改变一些东西。第一次修改参数

FormCollection form

IndexModel _oTechModel

,因为您已经从表单传递了IndexModel,所以它将自动绑定到参数_oTechModel。接下来删除行

IndexModel _oTechModel = new IndexModel();

然后改变

RedirectToAction("Index", _oTechModel);

应该是。查看文档

RedirectToAction("Index", new {_oTechModel= _oTechModel});