Asp.net mvc 4 从控制器中选择值

Asp.net mvc 4 从控制器中选择值,asp.net-mvc-4,knockout.js,Asp.net Mvc 4,Knockout.js,我得到了控制员的回应 [HttpGet] public JsonResult GetItems(string date) { IList<dough> modelList = new List<dough>(); modelList.Add(new dough { Type = "framed" }); modelList.Add(new dough { Type = "unframed" });

我得到了控制员的回应

    [HttpGet]
    public JsonResult GetItems(string date)
    {
        IList<dough> modelList = new List<dough>();
        modelList.Add(new dough { Type = "framed" });
        modelList.Add(new dough { Type = "unframed" });
        modelList.Add(new dough { Type = "soft" });
        return Json(modelList, JsonRequestBehavior.AllowGet);
    }
[HttpGet]
公共JsonResult GetItems(字符串日期)
{
IList modelList=新列表();
添加(新面团{Type=“framed”});
添加(新面团{Type=“unframed”});
添加(新面团{Type=“soft”});
返回Json(modelList,JsonRequestBehavior.AllowGet);
}
并以这种方式显示它

<table>
<thead>
    <tr><th>Waste</th></tr>
</thead>
<tbody data-bind="foreach: items">
    <tr>
        <td><select data-bind="options: $root.availableWastes, value: Waste, optionsText: 'Type'"></select></td>
    </tr>
</tbody>
</table>

浪费
结果

主要问题是如何显示从控制器获取的具有默认值的项?需要修复什么

控制器:

public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        return View();
    }

    [HttpGet]
    public JsonResult GetItems(string date)
    {
        IList<dough> modelList = new List<dough>();
        modelList.Add(new dough { Type = "framed" });
        modelList.Add(new dough { Type = "unframed" });
        modelList.Add(new dough { Type = "soft" });
        return Json(modelList, JsonRequestBehavior.AllowGet);
    }
}
class dough
{
    public string Type { get; set; }
}
公共类HomeController:控制器
{
//
//回家/
公共行动结果索引()
{
返回视图();
}
[HttpGet]
公共JsonResult GetItems(字符串日期)
{
IList modelList=新列表();
添加(新面团{Type=“framed”});
添加(新面团{Type=“unframed”});
添加(新面团{Type=“soft”});
返回Json(modelList,JsonRequestBehavior.AllowGet);
}
}
班级面团
{
公共字符串类型{get;set;}
}
Index.cshtml

@{
ViewBag.Title = "Index";
}

<script src="~/Scripts/knockout-2.2.0.js"></script>

<button data-bind="click: loadItems">Load</button>
<table>
<thead>
    <tr><th>Waste</th></tr>
</thead>
<tbody data-bind="foreach: items">
    <tr>
        <td><select data-bind="options: $root.availableWastes, value: Waste, optionsText: 'Type'"></select></td>
    </tr>
</tbody>
</table>

<script>
function MyViewModel() {
    var self = this;

    self.availableWastes = [{ Type: "framed", score: 0 },
                            { Type: "soft", score: 1 },
                            { Type: "unframed", score: 2 }];

    self.items = ko.observableArray();
    var date = new Date();
    self.loadItems = function () {
        var date = new Date();
        debugger;
        $.ajax({
            cache: false,

            type: "GET",

            url: "Home/GetItems",

            data: { "date": date },

            success: function (data) {

                var result = "";
                self.items.removeAll();
                $.each(data, function (id, item) {
                    self.items.push({ Waste: item.Type });
                });
            },

            error: function (response) {
                alert('eror');
            }
        });
    }
}
ko.applyBindings(new MyViewModel());
</script>
@{
ViewBag.Title=“Index”;
}
负载
浪费
函数MyViewModel(){
var self=这个;
self.availableWastes=[{Type:“framed”,分数:0},
{类型:“软”,分数:1},
{类型:“无框架”,分数:2}];
self.items=ko.observearray();
变量日期=新日期();
self.loadItems=函数(){
变量日期=新日期();
调试器;
$.ajax({
cache:false,
键入:“获取”,
url:“主页/GetItems”,
数据:{“日期”:日期},
成功:功能(数据){
var结果=”;
self.items.removeAll();
$。每个(数据、功能(id、项目){
self.items.push({Waste:item.Type});
});
},
错误:函数(响应){
警报(“eror”);
}
});
}
}
应用绑定(新的MyViewModel());

看起来您想要预先选择值

使用类型名称和废物将self.items更新为可观察的:

self.items.push({Waste:ko.observable(item.Type),Type:item.Type})

然后在选项值绑定中使用该选项:

截图: