Petar Mijović Petar Mijović - 5 months ago 54
ASP.NET (C#) Question

calling mvc function from razor html

I am working with MVC and i have question regarding calling function from my controller.

This is the function i want to call:

public ActionResult Index(string continent)
return View(db.Factories.ToList());
catch (Exception e)
string s = e.InnerException.Message;

return View( new FactoriesWController().GetFactoriesOnContinent(continent));

I want to call this function from
page, to be exact, this is drop down list.

I want to use selected item change to call the function and use selected item for function parameter. This is drop down list Razor code:

@Html.DropDownList("Region", new SelectList(new List<string>() {"Europe", "Asia", "North America", "South America", "Africa", "Australia", "World"}),
"Select Region",new { @class = "form-control" })

<input type="submit" />

So i want to use selected item from drop down list and then call the function by using selected item as parameter. I never coded in HTML, any help appreciated.


Put your form elements (SELECT and submit) inside a form tag. The action attribute of the form should point to your action method name. Rename your SELECT element name attribute value to match with your action method parameter name.

You can use the Html.BeginForm to generate the form tag.

@using (Html.BeginForm("Index", "home"))
        new SelectList(new List<string>() 
      {"Europe", "Asia", "North America", "South America", "Africa", "Australia", "World"}),
        "Select Region",
        new {@class = "form-control"})

    <input type="submit"/>

Assuming your Index action method is inside HomeController.

Now when user selects an option from the SELECT element and click submit the selection will be submitted to the Index action method and selected option will be available in the continent parameter.