asp.net-mvc 在表单提交时传递整个模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15527293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:39:58  来源:igfitidea点击:

Pass an entire model on form submission

asp.net-mvcmodelform-submithtml.hiddenfor

提问by jpo

I understand that I can use @Html.HiddenFor(m => m.parameter)and when the form is submitted, that parameter will be passed to the controller. My model has many properties.

我知道我可以使用,@Html.HiddenFor(m => m.parameter)并且在提交表单时,该参数将传递给控制器​​。我的模型有很多属性。

Is there a shorter way of passing the entire model at once to the controller or must I do it one by one each time?

是否有更短的方法将整个模型一次传递给控制器​​,还是必须每次都一个一个地执行?

回答by Forty-Two

The model will be passed to the controller in its entirety, but the values of properties that are not bound by input or hidden fields will be lost.

模型将完整地传递给控制器​​,但不受输入或隐藏字段约束的属性值将丢失。

You have to either bind the properties in the form on the client-side, or re-fetch the entity on the server-side.

您必须在客户端绑定表单中的属性,或者在服务器端重新获取实体。

You seem to be asking for something like @Html.HiddenFor(m => m.Model), and that is not possible. Sorry

您似乎在要求类似的东西@Html.HiddenFor(m => m.Model),但这是不可能的。对不起

One thing to keep in mind, if you have tons of hidden fields, you may be sending more data to the view than you really need. Consider employing view models

要记住的一件事是,如果您有大量隐藏字段,您可能会向视图发送比您真正需要的更多的数据。考虑使用视图模型

回答by Jason McGraw

For anyone else who looks at this you can do a @Html.EditorForModel() in a hidden div. You'd also have to use @Html.EditorFor(model => model.ObjectProperty) for each object property of the model.

对于查看此内容的任何其他人,您可以在隐藏的 div 中执行 @Html.EditorForModel()。您还必须为模型的每个对象属性使用 @Html.EditorFor(model => model.ObjectProperty)。

<div hidden="hidden">
  @Html.EditorForModel()
  @Html.EditorFor(model => model.ObjectProperty)
  @Html.EditorFor(model => model.ListOfObjectsProperty)
</div>

回答by Samy Sammour

You can check only the properties you want:

您只能检查所需的属性:

if (this.ModelState.IsValidField("Name"))
{
     // .....
}

instead of:

代替:

if (this.ModelState.IsValid)
{
     // .....
}

回答by sacha barber

The entire model will be posted if you are using a FORM element. Your elements using the Model obviously need to be inside the form element

如果您使用 FORM 元素,将发布整个模型。您使用模型的元素显然需要在表单元素内

You can also POST the form yourself say by using JQuery

您也可以使用 JQuery 自己说 POST 表单

See this other stack issue for that : jQuery AJAX submit form

请参阅其他堆栈问题:jQuery AJAX 提交表单

Have a close look at the anwser by "Alfrekjv"

仔细看看“Alfrekjv”的anwser

回答by Mike Perrenoud

This is already built in. Consider this model:

这已经是内置的。考虑这个模型:

public class MyModel
{
    public string PropertyA { get; set; }
    public string parameter { get; set; }
}

and now consider this action:

现在考虑这个动作:

[HttpPost]
public ActionResult PostSomeData(MyModel model)
{
}

MVC will leverage the FormCollectionand fill in the MyModelclass where it can. If you don't have the PropertyAin the form then it will be null. But since you have an inputfor the parameterproperty it will be filled in.

MVC 将尽可能地利用FormCollection和填充MyModel类。如果您PropertyA在表单中没有 ,那么它将是null。但是,因为你有一个inputparameter将在充满财产。