1

all I want to pass one javascript variable to Controller from MVC View from Form. This is my own selected text variable which I want to pass to controller so that I can retrieve and save in Database.Any help will be appreciated I am sharing the code which I want

View -->I am getting selected text value which I want to pass to controller

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

Now I want to pass this state variable to controller from an mvc form

@using (Html.BeginForm("Register", "Registration", FormMethod.Post))
{
    <input type="submit" value="Save" style="text-align:center"  />
}

Now, this is my controller Code which I want to retrieve

[HttpPost]
public ActionResult Register(RegisterModel Regmodel)
{
    if (ModelState.IsValid)
    {
        return View();
    }
}

Thanks @Rondles My issue is resolved now and thank you guys for ur quick resonse

2
  • use ajax to call the controller and pass your parameters Commented Mar 4, 2014 at 9:08
  • @zaki actually I don't want to go for Ajax because I have so may other controls also and through json I don't want to send .. Commented Mar 4, 2014 at 9:45

1 Answer 1

7

You could set up a hidden field in your form for the state (I assume state is a property on your RegisterModel?) and in your stateID.change function have it assigned the value to the hidden field for it to be passed on submit.

e.g:

Hidden field in the form can be set up with (inside your begin form code):

@Html.HiddenFor(model => model.StateID, new { id = hiddenStateID" })

Then your jQuery would be (untested, this is more pseduo code to give you the idea):

$(document).ready(function () 
 {  
        $("#stateID").change(function (){
             var state= $('#stateID :selected').text();
             getCities(state);
             $("#hiddenStateId").val(state);
        });
 });
Sign up to request clarification or add additional context in comments.

2 Comments

Can You please share some sample code to achieve this
Hi @Rondles thanks for your code it worked for me and my issue is resolved ...really appreciate your quick response

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.