通过MVC上的视图传递多个参数 [英] Passing multiple parameter through view on MVC

查看:52
本文介绍了通过MVC上的视图传递多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  <td style="width: 5%;" class="text-center">
                                                    <a href="AllUser/Add/@Model[i].UserId" title="Click here to edit"></a>
</td>





我们正在传递@



We are passing @

Model[i].UserId

用于常规编辑。现在要求我们需要通过

for regular edit .Now have a requirement that we need to pass one more Id through view that is

@Model[i].RoleId

的视图再传递一个Id。这两个值是从模型视图绑定我获得

.These two values are binded on view from model I am getting value for

Model[i].UserId

对相应的ActionResult.BTW的价值我是MVC的新手。我尝试了很多关于此的事情,如果有人有的话任何想法请帮助。

在此先感谢



我尝试过:



我试过了

AllUser / Add / @ Model [i] .UserId,@ Model [i] .RoleId

AllUser / Add / Id = @ Model [i] .UserId,rid = @ Model [i] .RoleId

AllUser / Add / Id = @ Model [i] .UserId& rid = @ Model [i] .RoleId

on the corresponding ActionResult.BTW I am new to MVC.I tried many things regarding this if anyone has any idea Please help.
Thanks In Advance

What I have tried:

I have tried
AllUser/Add/@Model[i].UserId,@Model[i].RoleId
AllUser/Add/Id=@Model[i].UserId,rid=@Model[i].RoleId
AllUser/Add/Id=@Model[i].UserId&rid=@Model[i].RoleId

推荐答案

对不起,我虽然你正在使用WebApi - 第三个解决方案(在底部)是使用MVC的解决方案...



如果对WebApi解决方案不感兴趣,请跳转至botto m ...



这样做的简单方法是在api控制器中使用[FromUri],如下所示:



改进了下面的答案(2)(但现在阅读第一个解决方案,了解第二个)



Sorry, I though you were using WebApi - the third solution (right at the bottom) is solution using just MVC...

If not interested in WebApi solution, jump to the bottom...

The easy way to do this is to use [FromUri] in your api controller as follows:

Improved answer (2) below (but read the first solution now to understand the second)

[Route("api/Values/Add")]
[HttpGet]
 public string Add([FromUri] int userId, [FromUri] int userRole)
 {
     return "Id: " + userId + ", RoleId:" + userRole;
 }





在视图中,您使用以下内容:





In the view you use the following:

<a href="api/Values/Add/?userId=@Model.UserId&userRole=@Model.UserRole" title="Click here to edit">Click here to edit</a>





以上内容可在benemanuel.net /测试中证明

在此示例中为userId和userRole只是作为字符串返回。



如果你想做更复杂的事情,可以考虑使用json - 如果你需要帮助,请联系我。 />


解决方案2 ==================



使用以下内容代替api控制器代码:





The above can be demonstrated at benemanuel.net/Testing
In this example the userId and userRole are simply returned as a string.

If you want to do more complex stuff, consider using json - if you want help with this, contact me.

Solution 2==================

Instead of above api controller code, use the following:

[Route("api/Values/Add2")]
[HttpGet]
   public string Add2([FromUri] User user)
   {
      return "Id: " + user.UserId + ", Role: " + user.UserRole;
   }





这假定一个名为User的类具有UserId和UserRole属性。





第三种解决方案 - 仅限MVC ====================



简单地使用:



锚点链接是:



This assumes a class called User that has UserId and UserRole properties.


Third Solution - MVC only====================

Simply use:

The anchor link is:

<a href="Testing/Testing/?userId=@Model.UserId&userRole=@Model.UserRole" title="Click here to edit">Click here to edit</a><br /><br /><br />







public ActionResult Testing(User user)
   {
       return View(user);
   }





再次假设类用户具有UserId和UserRole属性......



以下视图使用User作为传入的模型(如benemanuel.net/Testing所示 - 第三个链接向下...):





Again this assumes class User with UserId and UserRole properties...

The following view uses the User as a model passed in (as demonstrated as benemanuel.net/Testing - third link down...):

<h2>Testing</h2>

Id: @Model.UserId<br/><br />
UserRole: @Model.UserRole





很抱歉用WebApi搞砸你的头脑(尽管WebApi非常酷)有点事。)



Ben



Sorry for messing with your head with WebApi stuff (though WebApi is pretty cool for this kind of thing).

Ben


这篇关于通过MVC上的视图传递多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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