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

标签 c# asp.net-mvc asp.net-mvc-5

我正在尝试在我的 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 属性添加到您的模型中。

关于c# - 将 ViewModel 从 HttpGet 操作传递到 HttpPost 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32484837/

相关文章:

javascript - 将项目 ID 传递给 JS 函数

asp.net-mvc - 默认asp.net mvc 5模板中输入组插件之前的未知间隙

c# - 如何从 MVC 中的 DropDownList 中检索所选项目的文本和值

c# - 隐式运算符是否比 ToString() 方法具有更高的优先级?

c# - 与 Math.Round() 不一致

asp.net-mvc - knockout 映射没有更新我的模型

c# - 是否有等同于@HTML.dropdownlist 的复选框

c# - 用于解析的可扩展类型安全枚举类?

c# - 使用不同的通用参数实现相同的通用接口(interface) 2 次

asp.net-mvc - 为什么您已经或尚未转向 ASP.NET MVC?