ASP.NET MVC - Passing Parameters to the Controller

ASP.NET MVC - passing parameters to the controller

Your routing needs to be set up along the lines of {controller}/{action}/{firstItem}. If you left the routing as the default {controller}/{action}/{id} in your global.asax.cs file, then you will need to pass in id.

routes.MapRoute(
"Inventory",
"Inventory/{action}/{firstItem}",
new { controller = "Inventory", action = "ListAll", firstItem = "" }
);

... or something close to that.

How to pass a parameter to a controller from a cshtml view in mvc

Making a couple of changes should get this working.

First, if you want to use the routing for the url like this {controller}/{action}/{id}, change the parameter name in the controller action from UploadId to id:

public ActionResult DisplayUploadedFileContents(int id)

Next, it looks like you're linking from a different controller since in the error the requested URL is /UploadLogs/DisplayUploadedFileContents/89 (note UploadLogs is not DisplayUploadedFile).

Linking to the DisplayUploadedFile controller from a view that belongs to a different controller, you will need to use this overload taking 5 parameters:

@Html.ActionLink("Display File Contents", "DisplayUploadedFileContents", "DisplayUploadedFile", 
null, new { id = item.UploadId })

However, if you're accessing the controller from a view within the same controller you should be able to use this overload for ActionLink taking 3 parameters:

@Html.ActionLink("Display File Contents", "DisplayUploadedFileContents", new { id = item.UploadId })

Please refer to the ActionLink documentation

ASP.NET MVC - how to pass parameters to actions on a different controller

You need to use RedirectToAction("ActionName","ControllerName")

In your case its

return RedirectToAction("Error", "Error");

To pass values between controllers you must use TempData[]

Also you can pass data as parameters like below.

return RedirectToAction("Error", "Error", new { errorMessage = "Your Error Message Here" });`

Passing parameter to Controller on click of a button

With the code you shared, it will generate HTML markup like below

<input type="hidden" value="d.ID" name="Action:Delete" class="btn-primary btn-sm">

See, the value attribute is still a string d.ID! You probably want to use a @ prefix if you are trying to get the ID property value of the object d

<input type="hidden" value="@d.ID" name="Action:Delete" class="btn-primary btn-sm"/>

The server side code you shared won't event compile, specifically the part where you are trying to read the value from the posted form data and assign it to a variable. The below should work.

    [HttpPost]
public ActionResult SubmitEmailList()
{
if (Request.Form["Action:Delete"] != null)
{
var id = Request.Form["Action:Delete"];
// id is of string type. Use id as needed.
}
// to do : return something
}

Keep in mind that, the Request.Form["Action:Delete"] expression returns a string. If have a numeric value in that and want to convert it to int, You may use one of the helper methods like Convert.ToInt32 or even better, Int32.TryParse

I personally do not try to explicitly read the Request.Form, instead levearage the MVC model binding. I can directly use an int type as the parameter as well (So no need of explicit conversion from string to numeric type).

For this, your input element's name attribute value should match with your action method parameter. Also make sure you are specifying the attribute values inside quotes(single or double)

<form method="POST" action="@Url.Action("SubmitEmailList","YourControllerName")">
<input type="hidden" value="@d.ID" name="id" class="btn-primary btn-sm"/>
<button type="submit">Delete</button>
</form>

Now you may use

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SubmitEmailList(int id)
{
// to do : use id (which is int type_.
// to do : return something
}

Pass parameters to the Index() in Controller

try alter this:
return RedirectToAction("Index", "Inventories", new { customerID });

to this (if you are redirecting on action inside the same controller):

return RedirectToAction("Index", new { customerID = customerID });

or:
return RedirectToAction("Index", new { "customerID" = customerID });

left key must be the same as your route param name but it cant interfere with your variable name

MVC BeginForm - passing parameters to controller using GET

This behavior is in accordance with the HTML specifications, In particular for a form with method="get", (my emphasis)

Mutate action URL

Let destination be a new URL that is equal to the action except that its <query> component is replaced by query (adding a U+003F QUESTION MARK character (?) if appropriate).

So the query string value in your form's action attribute is replaced with the query string generated by the name/value pairs of the form controls.

Two options to solve this:

  1. Remove the new { type = ViewBag.BookType } from the BeginForm() method and add a hidden input for the parameter

    <input type="hidden" name="type" value="@ViewBag.BookType" />
  2. Create a custom route definition for the method so that type is added as a route parameter, not a query string value (note this must be before the Default route)

    routes.MapRoute(
    name: "Books",
    url: "Collections/Books/{id}/{type}",
    defaults: new { controller = "Collections", action = "Books" }
    );

    so that your current BeginForm() code will generate

    <form action="/Collections/Books/2/available" method="get">

and the form submit will result in a url of Collections/Books/2/available?search=searchterm

Unable to pass data from view to controller ASP.NET MVC 5

I solved the issue by changing phoneNumber to phoneNumberDto in PreTransfer method in the controller.



Related Topics



Leave a reply



Submit