【问题标题】:Pass ViewModel from HttpGet Action to HttpPost Action将 ViewModel 从 HttpGet Action 传递到 HttpPost Action
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 也试过用:@Html.HiddenFor(model => model.request) 不应该这样工作吗?
    • 嗯,请求是一个对象,但不确定它会粘在那里。你看过实际的 HTML 吗?无论如何,当您只使用状态时,为什么要发送任何 Request 对象的所有内容?并且可能是 POST 上的 ID。
    【解决方案2】:

    您不能将单个字段(如隐藏输入)绑定到整个对象。如果您将对象传递给它,它只会在其上调用 ToString 以获取表示形式,该表示形式很可能最终会成为类似 "{Namespace.To.Requests}" 的东西,而不是该对象中包含的实际数据。

    您可以为对象的 每个 属性显式创建输入,即:

    @Html.HiddenFor(m => m.request.Foo)
    @Html.HiddenFor(m => m.request.Bar)
    @Html.HiddenFor(m => m.request.Baz)
    

    或者您可以使用EditorFor 让 MVC 按照惯例为您执行此操作:

    @Html.EditorFor(m => m.request)
    

    当输入整个对象时,EditorFor 实际上会根据它可以从类中获取的信息,例如属性类型和应用的属性(DataTypeUIHint等)

    这意味着,它可能不会选择隐藏字段。您可以使用 HiddenInput 属性注释类中的属性:

    [HiddenInput]
    public string Foo { get; set; }
    

    但是,您不希望在实体类之类的东西上执行此操作,因为它会影响此类对表单的每次使用。在这些情况下,通常会使用视图模型,因为您可以创建一个单独的视图模型来代表您需要的任意数量的视图的实体,而不会影响应用程序的其他区域。

    您还可以利用编辑器模板来定义应为对象呈现的字段集EditorFor。通过简单地添加视图Views\Shared\EditorTemplates\Requests.cshtml,每当您使用Requests 的实例调用EditorFor 时,它将呈现该模板。然后,您可以在该视图中以任何您喜欢的方式呈现Requests 的字段。但是,同样,这是一个全局更改,会影响 EditorForRequests 实例的任何使用。

    很有可能,您最好的选择是直接在您的视图中直接为类上的每个属性手动调用Html.HiddenFor

    【讨论】:

    • 我不确定这是一个好方法。添加到请求的每个属性,您必须记住在此视图上添加隐藏输入。由于实际上没有使用任何字段,我不确定为什么视图中需要它们。
    • 嗯,是的,这并不理想,但鉴于 OP 提供的信息,这就是我能推荐的全部内容。
    【解决方案3】:

    您可以将对象作为模型传递给视图,但是当您想将对象的数据发送回控制器时,您不能发送对象,您必须使用具有相同属性名称的原始变量对象的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      相关资源
      最近更新 更多