【问题标题】:How can i bind objects field to text box?如何将对象字段绑定到文本框?
【发布时间】:2012-11-11 11:46:05
【问题描述】:

型号:

public class DishesModel
{
    public Dish DishToAdd{get; set;}
    public List<Dish> AllDishes{get; set;}
    public List<Category> AllCategories{get; set;}
    public List<Dish> SelectedDishes{get; set;}

    public DishesModel()
    {
        DishToAdd = new Dish();
    }

    public void AddDish()
    {
        if(DishToAdd!=null && !AllDishes.Contains(DishToAdd))
        {
            AllDishes.Add(DishToAdd);
            DishToAdd = null;
        }
    }

    public void RemoveSelected()
    {
        AllDishes.RemoveAll(d=>SelectedDishes.Any(m=>m.Name==d.Name));
        SelectedDishes.Clear();
    }

    public void SortDishes()
    {
        AllDishes.Sort();
    }

}

public class Dish:BaseEntity<Int32>
{
    public virtual String Name{get; set;}
    public virtual Decimal Price{get; set;}
    public virtual String Description{get; set;}
    public virtual IList<Category> Categories{get; set;}
    public virtual IList<Complex> Complexs{ get; set;}
}

剃须刀:

 @{
    KnockoutContext<MyMenu.Models.DishesModel> ko = Html.CreateKnockoutContext();
}

@{
    ViewBag.Title = "title";
}

<h2>Work with dishes.</h2>
@using(ko.Html.Form("AddDish", "Dishes", null, new{id="formDishes"}))
{
    @ko.Html.ListBox(m => m.AllDishes, new {size = 15}, dish => dish.Name).SelectedOptions(m=>m.SelectedDishes)
    @ko.Html.Button("Remove", "Remove", "Dishes").Enable(m => m.SelectedDishes.Count > 0)
    @ko.Html.Button("Sort", "Sort", "Dishes").Enable(m => m.AllDishes.Count > 1)
         <span>Name</span>         @ko.Html.TextBox(m=>m.DishToAdd.Name).ValueUpdate(KnockoutValueUpdateKind.KeyPress)
         <span>Price</span>
         @ko.Html.TextBox(m =>m.DishToAdd.Price).ValueUpdate(KnockoutValueUpdateKind.KeyPress)
         <span>Description</span>
         @ko.Html.TextBox(m => m.DishToAdd.Description).ValueUpdate(KnockoutValueUpdateKind.KeyPress)
         <button type="submit" @ko.Bind.Enable(m => m.DishToAdd.Name.Length > 0)>Add</button>

}

<script type="text/javascript">
    $('#formDishes').ajaxForm();
</script>

@ko.Apply(Model)

控制器:

public ActionResult AddDish(DishesModel model)
    {
        model.AddDish();
        return Json(model);
    }

未捕获的错误:无法解析绑定。 消息:TypeError:对象不是函数; 绑定值: value : DishToAdd().Name,valueUpdate : 'keypress' knockout-2.2.0.js:5

如何将DishToAdd.Name 绑定到TextBox?

提前致谢!

【问题讨论】:

    标签: c# asp.net-mvc-3 knockout.js knockout-mvc


    【解决方案1】:

    尝试使用字符串数据类型对对象进行类型转换

    【讨论】:

    • 决定using(var subModel=ko.With(m=&gt;m.DishToAdd)) { &lt;span&gt;Name&lt;/span&gt; @subModel.Html.TextBox(m=&gt;m.Name).ValueUpdate(KnockoutValueUpdateKind.KeyPress) &lt;span&gt;Price&lt;/span&gt; @subModel.Html.TextBox(m =&gt;m.Price).ValueUpdate(KnockoutValueUpdateKind.KeyPress) &lt;span&gt;Description&lt;/span&gt; @subModel.Html.TextBox(m =&gt; m.Description).ValueUpdate(KnockoutValueUpdateKind.KeyPress) &lt;button type="submit" @subModel.Bind.Enable(m =&gt; m.Name.Length &gt; 0)&gt;Add&lt;/button&gt; }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 2018-02-10
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多