我希望在单击ActionLink
时将模型和int
传递给控制器。
@Html.ActionLink("Next", "Lookup", "User", new { m = Model.UserLookupViewModel, page = Model.UserLookupViewModel.curPage }, null)
不起作用,相反,它会传递一个空的模型实例,这在使用new
时是可以预期的。
@Html.ActionLink("Next", "Lookup", "User", Model.UserLookupViewModel, null)
确实行得通。
控制器
[HttpGet]
public ActionResult Lookup(UserLookupViewModel m, int page = 0)
{
return this.DoLookup(m, page);
}
视图模型
public class UserLookupViewModel
{
public int curPage { get; set; }
[Display(Name = "Forenames")]
public string Forenames { get; set; }
[Display(Name = "Surname")]
public string Surname { get; set; }
[Display(Name = "DOB")]
public DateTime? DoB { get; set; }
[Display(Name = "Post code")]
public string Postcode { get; set; }
}
我如何将它们一起传递?Lookup
方法的参数与ActionLink中的命名属性匹配。
发布于 2014-02-03 22:09:56
您不能使用ActionLink来传递带有链接的属性,但您可以执行以下操作以获得相同的行为。
<form action="/url/to/action" Method="GET">
<input type="hidden" name="Property" value="hello,world" />
<button type="submit">Go To User</button>
</form>
如果您创建了一个帮助器来生成这些GET表单,您将能够像常规链接按钮一样设置它们的样式。我唯一要注意的是,页面上的所有表单都容易被修改,因此我不信任这些数据。当你到达你要去的地方时,我宁愿再拉一次数据。
我在创建搜索操作时使用了上面的技术,并希望保留搜索历史记录并保持后退按钮工作。
希望这能帮上忙
哈立德:)
附注:
这样做的原因是。
@Html.ActionLink("Next", "Lookup", "User", Model.UserLookupViewModel, null)
是因为ActionLink方法的参数列表被泛化为接受对象,所以它将接受任何内容。它将对该对象执行的操作是将其传递给RouteValueDictionary,然后尝试基于该对象的属性创建一个querystring。
如果您说上面的方法是有效的,那么您也可以尝试向视图模型添加一个名为Id的新属性,它将按照您所希望的那样工作。
发布于 2014-10-23 20:30:00
您必须将模型序列化为JSON字符串,并将其发送到控制器以转换为对象。
这是你的actionlink链接:
@Html.ActionLink("Next", "Lookup", "User", new { JSONModel = Json.Encode(Model.UserLookupViewModel), page = Model.UserLookupViewModel.curPage }, null)
在您的控制器中,您将需要一个方法将您的JSON数据转换为MemoryStream:
private Stream GenerateStreamFromString(string s)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
在ActionResult中,将JSON字符串转换为对象:
public ActionResult YourAction(string JSONModel, int page)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Model.UserLookupViewModel));
var yourobject = (UserLookupViewModel)ser.ReadObject(GenerateStreamFromString(JSONModel));
}
发布于 2014-02-03 22:15:17
为了安全起见,我强烈建议您使用表单来完成您在这里尝试做的事情。
@Html.ActionLink("Next", "Lookup", "User", new
{ Forenames = Model.UserLookupViewModel.Forenames,
Surname = Model.UserLookupViewModel.Surname,
DOB = Model.UserLookupViewModel.DOB,
PostCode = Model.UserLookupViewModel.PostCode,
page = Model.UserLookupViewModel.curPage }, null)
MVC将适当地映射属性;但是,这将使用您的url将值传递给控制器。这将显示所有人都可以看到的值。
为了安全起见,我强烈建议使用表单,特别是在处理像道布这样的敏感数据时。
我个人会这样做:
@using (Html.BeginForm("Lookup", "User")
{
@Html.HiddenFor(x => x.Forenames)
@Html.HiddenFor(x => x.Surname)
@Html.HiddenFor(x => x.DOB)
@Html.HiddenFor(x => x.PostCode)
@Html.HiddenFor(x => x.curPage)
<input type="submit" value="Next" />
}
如果需要,您可以在页面上使用多个此类表单。
然后,您的控制器接受post,但功能相同:
[HttpPost]
public ActionResult Lookup(UserLookupViewModel m, int page = 0)
{
return this.DoLookup(m, page);
}
https://stackoverflow.com/questions/21529092
复制相似问题