【发布时间】:2015-09-10 03:18:31
【问题描述】:
我正在尝试在我的 Action Get 中初始化一个 ViewModel,然后将其传递给 View,使用一些数据,然后将所有数据取回以在我的 Action Post 中使用,如下所示:
视图模型:
public class AddResponseModel
{
iDeskEntities db = new iDeskEntities();
public AddResponseModel()
{
RespTypeList = new SelectList(db.ResponseType.Where(e=>e.type != "Assignar" && e.type != "Reabrir"), "type", "type");
newRespList = new SelectList(db.Users, "id", "name");
}
[Required]
[DataType(DataType.Text)]
public string response { get; set; }
[HiddenInput]
public Requests request { get; set; }
[HiddenInput]
public string newResp { get; set; }
[DataType(DataType.Text)]
public SelectList newRespList { get; set; }
[HiddenInput]
public int RespType { get; set; }
[Required]
[DataType(DataType.Text)]
public SelectList RespTypeList { get; set; }
}
控制器:
[HttpGet]
public ActionResult AddResponse(int? id)
{
AddResponseModel model = new AddResponseModel();
model.request = db.Requests.Find(id);
return View("AddResponse", model);
}
[HttpPost]
public ActionResult AddResponse(Requests req, AddResponseModel model)
{
//Some code, where i wanna access again model.request that i //initiated on the GET action
return RedirectToAction("Dashboard", "BHome");
}
查看:
@using (Html.BeginForm("AddResponse", "Requests", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true)
@if (Model.request.state != 0)
{
<div class="form-group">
@Html.LabelFor(model => model.RespType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.RespType, Model.RespTypeList)
@Html.ValidationMessageFor(model => model.RespType)
</div>
</div>
}
<div class="form-group">
@Html.LabelFor(model => model.response, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.response)
@Html.ValidationMessageFor(model => model.response)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Adicionar Resposta" class="btn btn-default" />
</div>
</div>
</div>
}
还有办法吗?因为当我尝试在 Post 方法中使用“model.request”时,他会出现“null”
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-5