Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
  <td style="width: 5%;" class="text-center">
                                                    <a href="AllUser/Add/@Model[i].UserId" title="Click here to edit">class="fa fa-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
.These two values are binded on view from model I am getting value for
Model[i].UserId
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
Posted
Updated 24-Feb-18 6:29am
v2
Comments
David_Wimbley 24-Feb-18 0:41am    
You need to show your controller that you've created for these params that you are trying to have flow into your View.

Also, need to know if you've done anything to your RoutesConfig.cs (i think thats what its called, can't remember off top of my head, but the file with routes is in the App_Data folder) to configure customized routing within your project.

What you are attempting to do is pretty straight forward as long as the action in your controller is setup to accept 2 parameters.
Laiju k 24-Feb-18 6:24am    
yes action is accepting two parameters but I don't know the syntax on view.Sorry for the delay
David_Wimbley 24-Feb-18 17:21pm    
Thats not what i asked...Where is your code for the controller. Your question is about your controllers action yet you sis not provide the code for it.
Laiju k 26-Feb-18 3:01am    
public ActionResult Add(long ? id,long ? RoleId)
{

I cant send full code. Below solution worked for me I just needed the correct syntax.Thanks for helping and sorry if created any inconvenience.

1 solution

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>


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


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


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


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

Ben
 
Share this answer
 
v4
Comments
Laiju k 26-Feb-18 2:40am    
Thanks a lot third one is working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900