knockout下拉列表将选择传递给MVC5中的模型

[英]knockout dropdown list pass selection to model in MVC5


I am having a issue with a knockoutjs drop-down list, I have a MVC 5 application with this on my razor view, which is being bound from knockout.

我有一个knockoutjs下拉列表的问题,我有一个MVC 5应用程序与我的剃刀视图,这是从敲门绑定。

<select name="alddlname" id="alddl" 
    data-bind="
        options: authorityLevel, 
        optionsText: 'AuthorityLevel', 
        optionsValue: 'aid', 
        value: aid
    ">
</select> 

I would link to the use the selected item and pass it to a model attribute

我将链接到使用所选项目并将其传递给模型属性

public List<authorityLevel> authLevellistItems { get; set; }
public string SelectedAuthType { get; set; }

I would like to bind the model to the dropdown list so that selectedAuthType is then set to the value

我想将模型绑定到下拉列表,以便将selectedAuthType设置为该值

the dropdown is poppulated from knockout with this

这个下拉列表是从淘汰赛中推出的

var authorityLevelList = Html.Raw(JsonConvert.SerializeObject(ViewBag.authorityLevelDDL));

$(document).ready(function ()
{
    var vm = new AppViewModel();            
    vm.authorityLevel = ko.mapping.fromJS(@authorityLevelList);
    ko.applyBindings(vm);
});

thank you in advance

先谢谢你

1 个解决方案

#1


7  

OK so i have figured it out

好的,所以我已经弄清楚了

if i use the @html.DropDownListFor helper and still bind my data attributes from knockout I have a DropDown list that is populated by knockout json array object, but then also bind this to the MVC 5 model this then can be passed back to a controller and inserted to a database.

如果我使用@html.DropDownListFor帮助器并仍然从knockout绑定我的数据属性我有一个由knockout json数组对象填充的DropDown列表,但是然后也将它绑定到MVC 5模型然后可以传递回控制器并插入数据库。

// the data_bind part is the knockout.js part the rest is the razor c# view helper

// data_bind部分是knockout.js部分,其余部分是razor c#view helper

@Html.DropDownListFor(m => m.SelectedAuthType, 
        (SelectList)Model.authlevellistItems, 
new { id = "alddl", data_bind = " options: authorityLevel, optionsText: 'AuthorityLevel', optionsValue: 'aid'" }) 

hopefully this will help others and my question wasn't just a waste of time. Thanks A

希望这会帮助别人,我的问题不仅仅是浪费时间。谢谢


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2014/06/20/29871e30fa7e55599697833af1736a5.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号