我想创建发票创建视图。 我的viewModel如下:
public class InvoiceCreate
{
public IEnumerable<InvoiceItem> InvoiceItems { get; set; }
public IEnumerable<Recipient> Recipients { get; set; }
public Sender Sender { get; set; }
public Invoice Invoice { get; set; }
public string name { get; set; }
}
获取方法如下:
public ActionResult Create()
{
InvoiceCreate invoice = new InvoiceCreate();
invoice.InvoiceItems = (new Invoice.Model.InvoiceItem[20]).AsQueryable();
invoice.Recipients = (new Recipient[20]).AsQueryable();
invoice.Sender = new Sender() { Adrress="O.Karimov"};
invoice.Invoice = new Invoice.Model.Invoice();
return View(invoice);
}
我的观点如下:
@using (Html.BeginForm(new { id = "submitForm" }))
{
@Html.ValidationSummary(true)
@Html.TextBoxFor(model => model.name);
<div>
<div id="head">
<h1>Invoice</h1>
<div>
@Html.TextBoxFor(model => model.Invoice.InvoiceNumber, new { placeholder = "Invoice number" })
</div>... @for (var itemIndex = 1; itemIndex < Model.Recipients.Count(); itemIndex++)
{
<div class="recipient-row unvisible">
<button type="button" class="close" data-dismiss="alert" style="float: left; margin: 6px;">×</button>
@Html.TextBoxFor(model => model.Recipients.ElementAt(itemIndex).CustomerName, new { placeholder = "Contact name" })
@Html.TextBoxFor(model => model.Recipients.ElementAt(itemIndex).Email, new { type = "email", placeholder = "example@mail.ru" })
</div>
}
<div class="input-append date" data-form="datepicker" data-date="12-02-2012" data-date-format="dd-mm-yyyy">
@Html.TextBoxFor(model => model.Invoice.InvoiceDate, new { placeholder = "Invoice date", @Value = System.DateTime.Now.ToShortDateString(), @class = "grd-white", data_form = "datepicker" })
</div>
</div> <button type="submit" id="btnSave" name="Command" value="Save" class="btn btn-primary">Save</button>
<button type="submit" id="btnSubmit" name="Command" value="SaveDraft" class="btn btn-info">Save as draft</button>
<button type="submit" id="btnCancel" name="Command" value="Cancel" onclick="$('#submitForm').submit()" class="btn btn-danger">Cancel</button>
<input type="submit" name="name" value=" " />
</div>
}
发布方法如下:
[HttpPost]
public ActionResult Create(InvoiceCreate invoice, string Command)
{
switch (Command)
{
case "Save":
{
}
break;
case "SaveDraft":
{
}
break;
default:
return RedirectToActionPermanent("Index");
}
return RedirectToActionPermanent("Index");
}
我的问题是,当我点击Post方法中的任何按钮时,发票变量为空。我该如何解决? 提前谢谢。
答案 0 :(得分:0)
在ViewModel中,您需要一个构造函数,初始化复杂属性,如集合。
我认为这将“帮助”模型活页夹填充您的模型。