jquery - how to set model value with view element text in javascript (MVC 4) -


i have model set value of view element. , call method in controller model attribute value parameter in javascript.

whats best way of doing this?

something like:

@model.attribute = $(elementid).val(); //<- how can this?  '@html.action("getpeople", new { id = @model.attribute})'; 

i've been investigating , cannot find way of doing this. read , tried binding between view , model. when call @model.attribute value default. if change textbox value , model attribute not change.

@html.textboxfor(m => m.code, new { id = "txtattribute"}) 

i did model attribute binding in controller can see

 public actionresult index()         {             var model = new mymodel();             view("default", model);         } 

i've tried multiple things failed

var modelattribute = '@model.attribute'; modelattribute = '@viewdata["modelattribute"]'; modelattribute = '@html.raw(model.attribute)' modelattribute = '@html.raw(json.encode(model.attribute))' 

is possible? or have change algorithm?

update: model code

public class mymodel {      public string attribute{ get; set; }   } 

thanks lot, nelssen

you can set value of javascript variable model liks so: var modelattribute = "@model.attribute"; getting model data controller, far easiest way use hidden form elements:

//javascript function postdatatocontroller() {     var form = document.createelement("form");     form.setattribute("action", "submitformdata");     var hiddenelement = document.createelement("input");     hiddenelement.setattribute("name","attributename");     $(hiddenelement).val("attribute value");     $(form).append(hiddenelement);     $(form).submit();  } //c# [httppost] public void submitformdata(mymodel model) {     system.collections.specialized.namevaluecollection nvc = request.form;     model.attribute = nvc["attributename"]; } 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -