Callum311
Callum311

Reputation: 47

How do you retrieve a URL value in MVC?

I am having a slight little problem and wondering is there anyway that you could help me?

I am passing information using angularJs to an MVC controller. I am passing an Id of a user to the controller to allow the admin to change the user into an administrator. Here is the code for the view,

<tr ng-repeat="r in students | filter : searchStudent">
    <td><input type="text" name="Id" value="{{r.ID}}" readonly id="AdminIdText" /></td>
    <td>{{r.username}}</td>
    <td>{{r.Email}}</td>
    <td>{{r.AccountStatus}}</td>
    <td><a href="{{r.ID}}">test</a></td>
    <td><input type="submit" value="Make Admin" class="btn btn-warning" /></td>
</tr>

Here is the controller,

 public ActionResult AdminStatus(int id)
    {
        int Id = id;
        //Connection to the database
        var db = WebMatrix.Data.Database.Open("Database");
            db.Execute("UPDATE tblaccount SET AccountStatus= 1 WHERE ID =" + id);


        db.Close();
        return View("AddAdministrator");
    }
}

What I am wondering is, how do you retrieve the numeric value that has been passed through to the controller? This is because placing the information into a model is not working correctly.

Thanks in advance

Upvotes: 1

Views: 100

Answers (3)

Georg Patscheider
Georg Patscheider

Reputation: 9463

Use the MVC 'UrlHelper' to generate URLs that keep working even after controllers have been moved around:

@Url.Action("<ActionName>", "<ControllerName>", new { Area = "<AreaName>", parameter1 = parameter1Value, parameter2 = parameter2Value })

For example:

 @Url.Action("AdminStatus", "Admin", new { Area = "Admin", id = r.ID})

will hit the "AdminStatus" action in the "AdminController" in the Area named "Admin" and send a parameter named "id".

Upvotes: 1

Frank Hagenson
Frank Hagenson

Reputation: 1033

I think you need to do 2 things. Make the <a> tag look something like this:

<a href="Action/{{r.ID}}">

And add a controller method like this:

public ActionResult Action(int id)
{
    // Do something with the id
}

As long as the default route maps are set up in the MVC project, that should map the id value to the id parameter.

Upvotes: 2

praveen
praveen

Reputation: 1375

you can also use name attribute to get the id value to your controller method.

public void action(int Id)
{
   // your code
}

Upvotes: 0

Related Questions