Asp.net mvc 4 使用ActionLink将模型传递给控制器
两个简单的方法,创建一个新包,调用“create”,编辑包,调用“edit”方法。此方法调用“GetPackDetail”,获取包相关信息。编辑方法获取包信息并将其分配给模型,然后打开“编辑”页面 在编辑视图页面中,我试图将整个模型传递给区域控制器的“创建”方法。它工作得很好,当“编辑视图”页面在编辑模式下打开时,我可以通过该方法获得所有信息。但当用户通过调用packcontroller中的create方法来创建一个新包,然后输入包的名称,然后单击actionlink将该模型传递给区域控制器时,我在“Name”字段中得到null。对模型所做的更改似乎没有反映出这一点 包控制器代码段Asp.net mvc 4 使用ActionLink将模型传递给控制器,asp.net-mvc-4,Asp.net Mvc 4,两个简单的方法,创建一个新包,调用“create”,编辑包,调用“edit”方法。此方法调用“GetPackDetail”,获取包相关信息。编辑方法获取包信息并将其分配给模型,然后打开“编辑”页面 在编辑视图页面中,我试图将整个模型传递给区域控制器的“创建”方法。它工作得很好,当“编辑视图”页面在编辑模式下打开时,我可以通过该方法获得所有信息。但当用户通过调用packcontroller中的create方法来创建一个新包,然后输入包的名称,然后单击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);
}
将过帐表单更改为下面的代码。请务必替换控制器名称。您会注意到我使用了元素而不是操作链接。但是为你保留了引导类。只是让你知道,你结合了两种方式张贴。你应该使用一种或另一种方法。如果你需要进一步的帮助,请告诉我
@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(“创建区域”、“创建”、“区域”、模型,新建{@class=“btn btn success”})
正在传递原始模型,而不是您编辑的模型。那么我如何传递编辑的模型?有许多选项。使用javascript基于文本框构建url,并使用location.hef=url
或具有两个提交按钮,每个按钮具有唯一的名称
属性,然后在编辑()
发布方法中,检查按钮的值,并重定向到创建()
方法。您的方法应该是Create(字符串名称)
,而不是Create(iSPYPack模型)
为什么不使用表单呢?表格将通过整个模型。然后,您可以检查id是否>0,如果是,它是否已编辑,否则它是新的one@StephenMuecke我使用这种方法在一个表单中有两个提交按钮。这一切都很好,但问题是,如果我在表单中放置了其他按钮,它也会触发editpost方法。
@Html.ActionLink("Create New Pack", "Create", "ControllerOfCreateMethod", new { area = "Admin", Name = Model.Name }, new { htmlAttributes = new { @class = "btn btn-success" } })