我有一个有DDL的View。在DDL中选择一个值后,可以提交表单,并从Web Api服务中检索供应商的项目并重新加载页面。此表单按照Chrome设计的方式工作,但无法在IE中的FormCollection对象中找到DDL值。我错过了什么?
查看代码
@using AMCWeb.Models
@model AppointsViewModel
@{
ViewBag.Title = "Index";
}
<h2>Appraisal Appointment</h2>
@using (Html.BeginForm("GetAppointmentsByVendor", "AppraisalAppointment", FormMethod.Post, new {@id = "validationlist"}))
{
<br/>
<br/>
@Html.DropDownListFor(model => model.SelectedCompany, new SelectList(Model.Vendors.OrderBy(s => s.Company), "Id", "Company", Model.SelectedCompany), "- Select Vendor -")
<br/>
<input type="submit" value="Get Appointments" onclick="location.href='@Url.Action("GetAppointmentsByVendor", "AppraisalAppointment")'" />
<br/>
<table>
<tr>
<th>Id</th>
<th>Loan Number</th>
<th>State</th>
<th>Order Date</th>
<th>Apt Date</th>
<th>Est Due Date</th>
<th>Fees</th>
<th>MN Status</th>
</tr>
@foreach (AMCWeb.Models.AppraisalAppointment appraisalAppointment in Model.Appraisals)
{
//...load the table with data
}
</table>
}
控制器代码
public ActionResult GetAppointmentsByVendor(FormCollection formValues)
{
List<string> messages = new List<string>();
if (formValues["selectedCompany"].Trim() == string.Empty)
{
messages.Add("You must select a Vendor to begin.");
ViewBag.Messages = messages;
appointments.Vendors = _vendorRepository.Get();
return View("Index", appointments);
}
var vendorId = Convert.ToInt32(formValues["selectedCompany"]);
appointments.Appraisals = _appraisalAppointmentRepository.GetByVendor(vendorId);
appointments.Vendors = _vendorRepository.Get();
appointments.SelectedCompany = vendorId;
return View("Index", appointments);
}
来自IE的价值:
Chrome的价值:
更新:
看来vendorId正在通过。当我逐步完成代码时发现我发现的事实是它确实不在FormCollection对象中,然后代码突破了第一个&if; if语句&#39;因为它不在集合中,但是如果我继续单步执行代码,它会向右跳回第一行var vendorId = Convert.ToInt32(formValues["selectedCompany"]);
并且值就在那里。所以我修改了代码如下
public ActionResult GetAppointmentsByVendor(FormCollection formValues)
{
// to set up the model for reload
IEnumerable<AppraisalAppointment> appointment = new[] { new AppraisalAppointment() };
appointments.Appraisals = appointment;
appointments.Vendors = _vendorRepository.Get();
// for use in the tryparse
var selectedCompany = formValues["selectedCompany"];
int vendorId;
// for message deliver to the user
List<string> messages = new List<string>();
// if the vendorId is not an int then they didn't select one return
if (!Int32.TryParse(selectedCompany, out vendorId))
{
appointments.SelectedCompany = 0;
messages.Add("You must select a Vendor to begin.");
ViewBag.Messages = messages;
return View("Index", appointments);
}
// get the data for the user
appointments.Appraisals = _appraisalAppointmentRepository.GetByVendor(vendorId);
appointments.SelectedCompany = vendorId;
return View("Index", appointments);
}