Search code examples
javascriptasp.net-mvc-3

Assign Model property value from JavaScript value


I have a model with the following definition:

namespace ESimSolChemical.Models
{

    public class Employee
    {        
        public int EmployeeID{ get; set; }
        public string EmployeeName{ get; set; }
        public string Age{ get; set; }
        public string Address{ get; set; }

    }

I have Following JavaScript:

<script type="text/javascript">
    $(function () {
        $('#btnEmployeePiker').click(function () {
            var oParameter = new Object();
            oParameter.MultipleReturn = false;
            var oReturnObject = window.showModalDialog('/Employee/EmployeePiker/', oParameter, 'dialogHeight:470px;dialogWidth:550px;dialogLeft:400;dialogTop:100;center:yes;resizable:no;status:no;scroll:no');           
        });
    });
</script>

My Javascript oReturnObject contains two property Like :

oReturnObject.EmployeeID;
oReturnObject.EmployeeName;

Now I would like to assign:

@Model.employeeID=  oReturnObject.EmployeeID;

How can I accomplish this task?


Solution

  • You cannot set server side values with Javascript. You could bind these values to input fields like textboxes or hidden fields and then use Javascript to modify these values of these input fields.

    Take a look at these articles, it might help you out: