2015-11-11 56 views
0

調用創建新包「創建」的兩種簡單方法,並調用編輯包「編輯」方法。這個方法調用GetPackDetail來獲取與包相關的信息。編輯方法獲取包信息並將其分配給模型並打開「編輯」頁面。使用ActionLink將模型傳遞給控制器​​

在編輯視圖頁面我試圖將整個模型傳遞給區域控制器的「創建」方法。它工作正常,當在編輯模式下打開「編輯視圖」頁面時,我可以獲取該方法中的所有信息。但是,當用戶通過調用包控制器中的創建方法單擊創建新包時,我會在「名稱」字段中得到空值,然後輸入包的名稱,然後單擊動作鏈接將此模型傳遞給區域控制器。似乎對模型所做的更改沒有反映出來。

組控制器摘錄

public ActionResult Create() 
    { 
     iSPYPack model = new iSPYPack(); 

     return View("Edit", model); 
    } 

    public ActionResult Edit(int Id) 
    { 
     var model = GetPackDetail(Id);    

     return View("Edit", model); 
    } 

編輯視圖

@model iSPYCMS.Models.iSPYPack 

@using (Html.BeginForm()) 
{ 

    @Html.LabelFor(x => x.Name) 
    @Html.TextBoxFor(x => x.Name)             

}   

@Html.ActionLink("Create Area","Create","Area", Model, new { @class="btn btn-success"}) 

iSPYPack型號

public class iSPYPack 
{   
    public int Id { get; set; } 

    [Required(ErrorMessage = "Pack Name is required")] 
    [Display(Name = "Pack Name")] 
    public string Name { get; set; 
}  

區域控制器

public ActionResult Create(iSPYPack Model) 
    { 

     var ispypackMode = Model; 

     iSPYArea model = new iSPYArea(); 

     return View("Create", model); 
    } 
+0

'@ Html.ActionLink( 「創建區域」, 「創建」, 「區」,示範,新{@類= 「BTN BTN-成功」})'是通過原有的模式,而不是你已編輯。 –

+0

那我該如何通過編輯模型呢? –

+0

許多選項。使用javascript構建一個基於文本框的url,並使用'location.hef = url;'或者有2個提交按鈕,每個按鈕都有唯一的'name'屬性,然後在Edit()'post方法中,檢查按鈕並重定向到Create()方法是合適的。你的方法應該是'Create(string name)',而不是'Create(iSPYPack Model)' –

回答

0

更改發佈形式下面的代碼。請更換控制器名稱。而不是操作鏈接,你會注意到我已經使用了元素。但爲你保留你的引導類。請讓你知道,你結合了兩種發佈方式。你應該使用一種或另一種方法。讓我知道如果你需要進一步的幫助

@model iSPYCMS.Models.iSPYPack 

@using (Html.BeginForm("Create", "ControllerNameOfCreateMethod", FormMethod.Post)) 
{ 

    @Html.LabelFor(x => x.Name) 
    @Html.TextBoxFor(x => x.Name)  

<input type="submit" value="Create New Pack" class="btn btn-success"/>           

}   

但是,如果您的需求,你需要行動鏈接,這將有所幫助。

@Html.ActionLink("Create New Pack", "Create", "ControllerOfCreateMethod", new { area = "Admin", Name = Model.Name }, new { htmlAttributes = new { @class = "btn btn-success" } }) 
相關問題