一个简单的问题......当用户没有输入值(可选值)并提交表单时如何处理异常?我正在寻找一些简单的方法来实现..
我收到“对象引用未设置为对象实例”之类的异常。
下面是我的控制器
public ActionResult GenerateReport(FormCollection Form)
{
int type_id = Convert.ToInt32(Form["type_id"].ToString());
int ClientId = Convert.ToInt32(Form["SelectedClientId"].ToString());
DateTime Start_Date = Convert.ToDateTime(Form["Start_Date"].ToString());
DateTime End_Date = Convert.ToDateTime(Form["End_Date"].ToString());
//StringBuilder sb = new StringBuilder();
// var cm = new ContractManager();
var cm = db.contracts.Where( c=>c.tb_contract_type_id == type_id ).ToList();
return View(cm);
}
下面是视图
@using (Ajax.BeginForm("GenerateReport", "Home", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "Generate-Report" }, new { id = "Form" }))
{
<div class="editor">
<div class="editor-label">
Client Name
</div>
<div id="Clients" class="editor-field">
@Html.Partial("~/Views/Contracts/SelectClient.cshtml", Model)
</div>
</div>
<div class="editor">
<div class="editor-label">
@Html.LabelFor(model => model.cmContracts.tb_contract_type_id)
</div>
<div class="editor-field">
@Html.DropDownList("type_id", ViewBag.contracttypes as SelectList, new { style = "width: 150px;" })
</div>
</div>
<div class="editor">
<div class="editor-label">
Start Date
</div>
<div class="editor-field">
@Html.TextBox("Start_Date", "", new { data_datepicker = true })
</div>
</div>
<div class="editor">
<div class="editor-label">
End Date
</div>
<div class="editor-field">
@Html.TextBox("End_Date", "", new { data_datepicker = true })
</div>
</div>
<p>
<input style="margin:20px auto 0px 120px; " type="submit" value="Generate" />
</p>
}