C# 将隐藏对象从视图传递到控制器
我有一个控制器将一个由列表和对象客户端组成的视图模型发送到一个视图。 视图将在网格中显示列表,但隐藏对象客户端。 以下是视图:C# 将隐藏对象从视图传递到控制器,c#,asp.net-mvc,C#,Asp.net Mvc,我有一个控制器将一个由列表和对象客户端组成的视图模型发送到一个视图。 视图将在网格中显示列表,但隐藏对象客户端。 以下是视图: @model .Business.BusinessModels.MatchesClientViewModel @{ ViewBag.Title = "SaveClient"; } <h2>SaveClient</h2> <h3> The info captured matches @Model.ClientMatc
@model .Business.BusinessModels.MatchesClientViewModel
@{
ViewBag.Title = "SaveClient";
}
<h2>SaveClient</h2>
<h3>
The info captured matches @Model.ClientMatches.Count()
</h3>
@using (Html.BeginForm("SaveClient", "Client", FormMethod.Post))
{
@Html.AntiForgeryToken()
WebGrid grid = new WebGrid(Model.ClientMatches);
@grid.GetHtml(columns: new[]
{
grid.Column("Name"),
grid.Column("Surname"),
grid.Column("Email"),
})
@Html.Hidden("client", Model.client)
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
}
但是,从视图发送到控制器的视图模型为空。知道如何正确地将视图模型的隐藏部分传递给控制器吗?如果要将整个对象作为隐藏对象传递,则必须为类的每个属性添加隐藏字段,如下所示: 假设模型客户机是这样的
public class Client
{
public string Id { get; set; }
public string SomeAttribute { get; set; }
\\ ......
}
@Html.HiddenFor(m => m.client.Id, Model.client.Id)
@Html.HiddenFor(m => m.client.Someattribute, Model.client.Someattribute)
@Html.HiddenFor(m => m.clientId., Model.clientId)
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SaveClient(MatchesClientViewModel matchesClientViewModel, string clientId)
{
*some actions*
return View();
}
为了将值传递给操作,应该将每个属性作为隐藏字段添加到表单中,如下所示
public class Client
{
public string Id { get; set; }
public string SomeAttribute { get; set; }
\\ ......
}
@Html.HiddenFor(m => m.client.Id, Model.client.Id)
@Html.HiddenFor(m => m.client.Someattribute, Model.client.Someattribute)
@Html.HiddenFor(m => m.clientId., Model.clientId)
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SaveClient(MatchesClientViewModel matchesClientViewModel, string clientId)
{
*some actions*
return View();
}
另一种方法是将模型更改为以下内容:
public class MatchesClientViewModel
{
public List<ClientMatch> ClientMatches { get; set; }
public string clientId { get; set; }
\\ .....
}
或者,如果不想更改ViewModel,只需添加一个隐藏字段,就像为客户端id的对象添加隐藏字段一样,并将其作为额外参数传递给控制器
@Html.HiddenFor('clientId', Model.client.Id)
让你的行动像这样
public class Client
{
public string Id { get; set; }
public string SomeAttribute { get; set; }
\\ ......
}
@Html.HiddenFor(m => m.client.Id, Model.client.Id)
@Html.HiddenFor(m => m.client.Someattribute, Model.client.Someattribute)
@Html.HiddenFor(m => m.clientId., Model.clientId)
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SaveClient(MatchesClientViewModel matchesClientViewModel, string clientId)
{
*some actions*
return View();
}
客户端是一个对象吗?或者它只是一个id?@AnastasiosSelmanis:它是一个对象,那么我担心您必须像这样传递对象的每个字段:@Html.HiddenFor(m=>m.client.id,Model.client.id),@Html.HiddenFor(m=>m.client.someattribute.,Model.client.someattribute)等等,它将映射到您的表单提交中。一般来说,最好只使用client.id,因为这可能是您在后端需要的,或者如果您想要对象,请从后端重新获取客户端对象。然后我还必须更改我的操作、SaveClient和参数?我没有使用MatchesClientViewModel作为参数,而是使用someattribute type?@tabby,不,模型绑定器应该能够确定这些字段属于模型的
客户机
字段,并根据属性值为您创建它。但是,只有当字段是强类型时,这才是正确的。仅仅object
很可能不起作用最好只发布客户端ID并从缓存或数据库检索对象服务器端(并检查登录用户是否确实有权访问此客户端ID)。不要天真地相信表单中提交的内容,即使它来自隐藏的输入。你可能还想看看防伪标识:@AlexSanséau在我对问题的评论中提到,他能做的最好的事情就是你提到的。不是为了认证,而是为了良好的实践。我刚刚发布了一个答案,其中包括一种按照他的要求去做的方法。