这些天我正在学习使用knockout.js并尝试使用mvc4中的kMVC包生成动态字段。以下是我的观点:
@using PerpetuumSoft.Knockout
@model LotusDiamond.Models.NewAttribute
@{
ViewBag.Title = "NewAttribute";
var ko = Html.CreateKnockoutContext();
}
<div class="main-wrap">
<div>
@Html.ActionLink("Back to List", "Index")
</div>
<h2>NewAttribute</h2>
<div id="tabs1-general">
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>NewAttribute</legend>
<div class="editor-label">
@Html.LabelFor(model => model.AttributeId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AttributeId)
@Html.ValidationMessageFor(model => model.AttributeId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AttributeName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AttributeName)
@Html.ValidationMessageFor(model => model.AttributeName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AttributeDatatype)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AttributeDatatype)
@Html.ValidationMessageFor(model => model.AttributeDatatype)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.IsActive)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.IsActive)
@Html.ValidationMessageFor(model => model.IsActive)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AttributeType)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AttributeType)
@Html.ValidationMessageFor(model => model.AttributeType)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@using (var values = ko.Foreach(m => m.AttributeValues))
{
<div class="editor-field">
@values.Html.TextBox(m => m.AttributeVal).UniqueName()
</div>
<div>@ko.Html.HyperlinkButton("Delete", "DeleteValue", "Product", new { valIndex = values.GetIndex() })</div>
}
@ko.Html.HyperlinkButton("Add New", "AddValue", "Product")
</div>
</div>
</div>
@ko.Apply(Model)
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
模型类 -
public class NewAttribute
{
[MaxLength(20)]
public string AttributeId { get; set; }
[Required]
[Display(Name = "Attribute Name")]
[MaxLength(100)]
public string AttributeName { get; set; }
[Required]
[Display(Name = "Datatype")]
[MaxLength(50)]
public string AttributeDatatype { get; set; }
[Required]
[Display(Name = "Is Active")]
public bool IsActive { get; set; }
[Required]
[Display(Name = "Attribute Type")]
[MaxLength(30)]
public string AttributeType { get; set; }
public List<AttributeValue> AttributeValues { get; set; }
public void AddValue()
{
AttributeValues.Add(new AttributeValue());
}
public void DeleteValue(int valIndex)
{
if (valIndex >= 0 && valIndex < AttributeValues.Count)
{
AttributeValues.RemoveAt(valIndex);
}
}
}
控制器 -
public ActionResult NewAttribute()
{
return View();
}
[HttpPost]
public ActionResult NewAttribute(NewAttribute newAttr)
{
if (ModelState.IsValid)
{
//Code for saving data
}
return View(newAttr);
}
public ActionResult AddValue(NewAttribute newAttr)
{
newAttr.AddValue();
return View(newAttr);
}
public ActionResult DeleteValue(NewAttribute newAttr, int valIndex)
{
newAttr.DeleteValue(valIndex);
return View(newAttr);
}
当我运行页面时,它会出现以下错误:
Error: Unable to parse bindings.
Message: ReferenceError: AttributeValues is not defined;
Bindings value: foreach: AttributeValues @ http://localhost:3138/Scripts/knockout-2.2.1.js:57
请帮助我,我做错了什么。我已经搜索了很多,但没有找到解决这个问题的方法。
提前致谢!!