簡體   English   中英

POST ASP.NET MVC 4后數據丟失

[英]Data lost after POST ASP.NET MVC 4

我一直在解決表單提交問題,遇到了我不知道的問題。 我知道這個問題已經發布了很多次,但是我似乎無法解決。

模型:

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方法的第一行是

IndexModel _oTechModel = new IndexModel();

然后,您使用此“空”模型重定向到索引頁面。 (在Update語句的情況下,您沒有為_otechModel分配任何值)

您需要改變幾件事。 第一次更改參數

FormCollection form

IndexModel _oTechModel

因為您已經從表單傳遞了IndexModel,所以它將自動綁定到參數_oTechModel。 接下來刪除該行。

IndexModel _oTechModel = new IndexModel();

然后改變

RedirectToAction("Index", _oTechModel);

它應該是。 查看文檔

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM