简体   繁体   中英

Data passing from Javascript to MVC controller

I need to pass data from javascript to a controller in MVC app. How can this be achieved? I would like to set a ViewModel property and then read that in the controller. My JavaScript:

function DoInfo@(i.ToString())() {
         $("#SelectedSession").val($("#sessionid@(i.ToString())").val());
         alert($("#sessionid@(i.ToString())").val());
         $("#PageController").val(70);
         $("#formID").submit();
    }

I get the alert with right sessionid but it is null when I read it in the controller as viewModel.SelectedSession. But PageController is righlty set so I assumed I can use the same way to do the SelectedSession.

 if (viewModel.PageController == 70) 
        {
            pass = new PassingData();
            pass.personid = TSSessionService.ReadPersonId(viewModel.SelectedSession);
            TempData["pass"] = pass;

            return RedirectToAction("Index", "Info");
        }

Any clues?

Add SelectedSession to your view as a field (maybe a hidden field if you do not want to show it to whoever is using the application)

On your view, inside the form being submitted, add

<%=Html.HiddenFor(Model=>Model.SelectedSession)%>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM