Skip to main content
 首页 » 编程设计

c#之将 ViewModel 从 HttpGet 操作传递到 HttpPost 操作

2024年02月05日32xing901022

我正在尝试在我的 Action Get 中初始化一个 ViewModel,然后将其传递给 View,使用一些数据,然后获取所有数据以在我的 Action Post 中使用,如下所示:

View 模型:

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

Controller :

[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”

请您参考如下方法:

绑定(bind)只会发生在实际使用的字段上。如果您没有在 View 中使用请求属性,最好将 ID 隐藏在输入中,然后在您的 POST 中再次加载到服务器上。对于状态,只需将 bool 属性添加到您的模型中。