将参数从@ Html.ActionLink传递给View [英] Passing parameter from @Html.ActionLink to View

查看:104
本文介绍了将参数从@ Html.ActionLink传递给View的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我启动了一个MVC应用程序并修改了标准注册过程(我使用Identity 2.0并计划使用OWin进行扩展)

I在AspNetUsers表中添加了字段,我根据我捕获的新信息调整了所有必要的条件,并且它完成了所有工作。

我想要和不知道怎么做的是在导航中添加两个@ Html.ActionLink项目指向相同的注册视图,但每个都传递一个不同的值作为Role的参数(例如,一个链接发送值2意味着我的候选人,其他值3意味着雇主;至少我希望保存在表中)。另外,如果可能的话,我希望有条件地显示有关参数值的表格中的字段(例如,如果角色值为2以显示生日字段,如果3显示公司名称等等)。

另外我想要保存也以相同的形式发送刚刚创建的UserID(如何捕获它)和作为参数的角色AspNetUserRoles,其中包含字段UserID和RoleID



感谢您的任何提示。

解决方案

查看ActionLink的任何文档或教程,它将解释如何传递参数



 @ Html.ActionLink(点击,MyAction,MyController,新{role = 2},null)





这将在网址中添加?role = 2,并且您的操作需要能够绑定到角色参数才能填充它,例如



 public ActionResult MyAction(int role)
{
//
}





有条件地展示事物,保存数据,将数据传递给动作等都是MVC的基本方面,而论坛并不是学习这些东西的最佳场所。通过一些适当的MVC教程,他们将解释所有基础知识



http: //www.asp.net/mvc [ ^ ] < br $> b $ b

http://www.asp.net/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-1 [ ^ ]


Hi
I started an MVC application and I modified the standard registration process (I use Identity 2.0 and plan also to extend with OWin)
I added fields to AspNetUsers table, I adapted all necessary to new information I capture and all it works.
What I want and don't know how to do is to add in navigation two @Html.ActionLink items that points to the same Registration view, but each passing a different value as parameter for Role (e.g. one link to send value 2 meaning for me candidate, other value 3 meaning employer; at least that I want to save in table). Also I want if possible to conditionally display fields in form regarding to parameter value (e.g. if Role value is 2 to display Birthday fields, if 3 to display Companyname and so on).
Also I want to save also at same form send the just created UserID (how to capture it) and the Role that come as parameter in the table AspNetUserRoles, that have as fields UserID and RoleID

Thank you for any hint.

解决方案

Look at any documentation or tutorial for ActionLink and it will explain how you pass parameters

@Html.ActionLink("Click", "MyAction", "MyController", new { role = 2 }, null)



That will add "?role=2" to the url, and your action will need to be able to bind to the "role" param in order for it to be populated, eg

public ActionResult MyAction(int role)
{
//
}



Conditionally displaying things, saving data, passing data to actions etc are all basic aspects of MVC, and a forum isn't the best place to learn these things. Go through some proper MVC tutorials and they'll explain all the basics

http://www.asp.net/mvc[^]

http://www.asp.net/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-1[^]


这篇关于将参数从@ Html.ActionLink传递给View的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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