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