Rahul
Rahul

Reputation: 127

How to Pass our own Parameter from Html.BeginForm

I want to pass some additional Parameter from My MVC Form to Controller. Can someone guide me how to achieve this. Now I want to Pass one more parameter Like

StateName and CityName with value from Form so that I can retrieve in my controller to save this. Can someone Guide me how to achieve this? My View:

@using (Html.BeginForm("Register", "EmployeeRegistration", new }, FormMethod.Post))
{
    <input type="submit" value="Register Emp Information" style="text-align:center">
}

Thanks everyone I am able to pass but my problem is I am getting some value from jquery which I want to pass from From sharing more code to understand what is my problem

I have a dependent DropDownList Named State and City

@Html.DropDownList(m=>m.stateDDL, new SelectList(@ViewBag.stateDDL), "Select a State", new { id = "stateID" })

Now onchange event of this DropDownList I am getting the selected value with this code

$("#stateID").change(function () {
    var state = $('#stateID :selected').text();
    alert(state);
});

Now I want to pass this selected Name from Form to controller, Can someone tell me how to pass this state value

Controller

[HttpPost]
public ActionResult Register(EmployeeRegistration empRegmodel,FormCollection form)
{
    var state = form["stateDdl"].ToString();

    if (ModelState.IsValid)
    {
        Er.registerEmpInfo(empRegmodel);
        return RedirectToAction("HomeScreen", "HomeScreen");
    }
    Er.getCountry();
    Er.getCity();
    Er.getState();
    ViewBag.countryddl = Er.country;
    ViewBag.cityddl = Er.city;
    ViewBag.stateddl = Er.state;
    return View("EmployeeRegistration");
}

Upvotes: 1

Views: 1980

Answers (1)

Nilesh Gajare
Nilesh Gajare

Reputation: 6398

Store the values in Hidden fields and retrieve it at controller using FormCollection

Update no need of jquery here

instead try this : add name attribute

 @Html.DropDownList(m=>m.stateDDL, new SelectList(@ViewBag.stateDDL), "Select a State", new { id = "stateID",name="stateID" })

Controller

 public ActionResult Index(FormCollection form)
   {
       var stateid=form["stateID"].ToString();
   } 

Upvotes: 1

Related Questions