POST ASP.NET MVC 4后数据丢失

时间:2014-05-22 05:33:58

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我一直在处理表单提交,我碰到了一些我无法弄清楚的事情。我知道这个问题已被多次发布,但我似乎无法解决问题。

MODEL:

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>
}

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

2 个答案:

答案 0 :(得分:2)

post方法的第一行是

IndexModel _oTechModel = new IndexModel();

然后使用此“空”模型重定向到索引页面。 (在更新语句的情况下,您没有为_otechModel分配任何值)

答案 1 :(得分:0)

你需要改变一些事情。 首先更改参数

FormCollection form

IndexModel _oTechModel

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

IndexModel _oTechModel = new IndexModel();

然后改变

RedirectToAction("Index", _oTechModel);
它应该是。查看Documentation

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