0
UPDATE:通过将name属性添加到添加的select标签来解决此问题,以便在提交时将它添加到formelement中。将生成的字段绑定到剃须刀视图中的模型
我有一个局部视图,get的传递一个拥有外键的模型。局部视图的唯一目的是在数据库中为该模型创建一个新对象。我创建了一个基于模型之外的字段的下拉列表,现在当我发布表单时,该字段不包含在api文章中以创建记录。 (对于那些熟悉的,是的,这是相当多的接触例如开箱,我试图把它扩大一点,可以使用一些帮助)
<form id="addContact" data-bind="submit: createContactFromForm">
@Html.ValidationSummary(true)
<fieldset>
<legend>Contact</legend>
@Html.EditorForModel()
<div class="editor-label"><label>Store:</label></div>
<div class="editor-field" id="storediv">
<select id="StoreId" **name="StoreId"** data-bind="options: stores, optionsText: 'Name', optionsValue: 'Id', optionsCaption: 'Choose...'"></select>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
</form>
我怎样才能获得存储领域成为表单提交模型的一部分?我重写提交以在knockoutjs视图模型中调用createContactFromForm函数。
更新与被称为视图模型的一部分:
self.createContactFromForm = function (formElement) {
// If valid, post the serialized form data to the web api
$(formElement).validate();
if ($(formElement).valid()) {
$.post("api/contacts", $(formElement).serialize(), "json")
.done(function (newContact) {
self.contacts.push(newContact);
$('#addContact')[0].reset();
});
}
}
服务器端模式:
public Contact()
{
this.Created = DateTime.Now;
this.Emails = new List<Emails>();
}
[ScaffoldColumn(false)]
public int Id { get; set; }
[Required, MaxLength(256)]
public string FirstName { get; set; }
[Required, MaxLength(256)]
public string LastName { get; set; }
[ScaffoldColumn(false)]
public string Name { get { return string.Concat(this.FirstName, " ", this.LastName); } set { } }
[MaxLength(256)]
public string EmailAddress {
get
{
return this.Emails.Count == 0 ? string.Empty : this.Emails[0].EmailAddress;
}
set
{
if (this.Emails.Count == 0)
{
this.Emails.Add(new Emails());
}
this.Emails[0].EmailAddress = value;
}
}
[MaxLength(50)]
public string PhoneNumber { get; set; }
[MaxLength(256)]
public string Address { get; set; }
[MaxLength(256)]
public string City { get; set; }
[MaxLength(50)]
public string State { get; set; }
[MaxLength(256)]
public string ZipCode { get; set; }
[Required]
[ScaffoldColumn(false)]
public int StoreId { get; set; }
public Store Store { get; set; }
[ScaffoldColumn(false)]
public DateTime Created { get; set; }
public virtual IList<Emails> Emails { get; protected set; }
它是一个完整的代码?我看不到淘汰赛视图模型? –
已更新,并调用了部分视图模型。只是一个抓住表单元素的帖子。 – utd1878
你的服务器侧面模型是怎样的? – nemesv