ASP.NET MVC ActionLink的所传递模型 [英] ASP.NET MVC Passing models by ActionLink

查看:195
本文介绍了ASP.NET MVC ActionLink的所传递模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过一个模型和 INT 来后,单击控制器中的 ActionLink的

I would like to pass a model and an int to a controller upon clicking an ActionLink.

@Html.ActionLink("Next", "Lookup", "User", new { m = Model.UserLookupViewModel, page = Model.UserLookupViewModel.curPage }, null)

不工作,而是通过模型,其中一个会使用时期望

@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; }
}

我怎样才能通过他们在一起呢?在查找方法的参数在ActionLink的匹配命名属性。

How can I pass them together? The Lookup method's arguments match the named properties in the ActionLink.

推荐答案

您不能使用ActionLink的同一个链接传递的属性,但你可以执行以下操作来获得相同的行为。

You can't use an ActionLink to pass properties with a Link but you can do the following to get the same behavior.

<form action="/url/to/action" Method="GET">
  <input type="hidden" name="Property" value="hello,world" />
  <button type="submit">Go To User</button>
</form>

如果您创建一个帮助产生这些获取的形式,您将能够他们的风格像他们经常链接按钮。我告诫的唯一的事情是,页面上的所有的形式很容易受到修饰,所以我不会相信这些数据。我宁愿只是再次拉取数据,当你到你要去哪里。

If you create a helper to generate these GET forms, you will be able to style them like they are regular link buttons. The only thing I caution against is that ALL forms on the page are susceptible to modification so I wouldn't trust the data. I'd rather just pull the data again when you get to where you are going.

我用创建搜索行动时,上面的技术,并希望保留搜索历史,并保持后退按钮的工作。

I use the technique above when creating search actions and want to retain a search history and keep the back button working.

希望这有助于

哈立德:)

P.S。

之所以这样工作的。

@Html.ActionLink("Next", "Lookup", "User", Model.UserLookupViewModel, null)

是因为ActionLink的方法的参数列表推广到采取的目标,所以它会采取什么。它的作用与该对象是它传递给 RouteValueDictionary ,然后尝试基于对象的属性来创建一个查询字符串。

Is because the parameter list of the ActionLink method is generalized to take an object, so it will take anything. What it will do with that object is pass it to a RouteValueDictionary and then try to create a querystring based on the properties of that object.

如果你说的方法正在上面,你也可以只是尝试添加一个新的属性称为视图模型的标识,它会像你想它。

If you say that method is working above, you could also just try adding a new property to the viewmodel called Id and it will work like you wanted it to.

这篇关于ASP.NET MVC ActionLink的所传递模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆