模型的继承 属性 总是返回 Null 值
Inherited Property of Model is always returning Null Value
我有一个视图,它在页面左侧显示一个单选按钮列表,它还在页面右侧加载一个部分视图,其中包含在弹出窗口中显示报告之前要选择的各种过滤器 window.
问题是,当按下提交按钮时,名为 'ReportName' 的 属性 总是 return 一个 Null 值,而不是 return 选定单选按钮的字符串值其他属性,例如 Branchcode、PeriodFrom、PeriodTo return 正确。
what should i do to get proper string value such as AppointmentSummary, SplcodeRegSummary, or SplcodeReg when its radiobutton is selected.
在此先感谢您的帮助。
型号:
public class ReportModel
{
[Required(ErrorMessage = "Please select a Report!")]
public string ReportName { get; set; }
}
public class ReportFilters :ReportModel //inherited
{
[Required(ErrorMessage="Please select Branch name!")]
public int BranchCode { get; set; }
[Required(ErrorMessage = "Please specify 'Period from' date!")]
[DataType(DataType.Date)]
public DateTime PeriodFrom { get; set; }
[Required(ErrorMessage = "Please specify 'Period to' date!")]
[DataType(DataType.Date)]
public DateTime PeriodTo { get; set; }
}
控制器:
public ActionResult MIS1()
{
ViewBag.Branch = (from c in BranchList().AsEnumerable()
orderby c["BranchName"]
select new { BranchCode = c["BranchCode"], BranchName = c["BranchName"] }).ToList();
return PartialView();
}
public ActionResult MISPopup(ReportFilters rf)
{
if (ModelState.IsValid)
{
return View(rf);
}
return View();
}
查看:
<TABLE>
<TR>
<td>
@Html.RadioButtonFor(model => model.ReportName, "AppointmentSummary", new { id = "AppointmentSummary" })
@Html.Label("AppointmentSummary", "Appointment Summary")<br />
@Html.RadioButtonFor(model => model.ReportName, "SplcodeRegSummary", new { id = "SplcodeRegSummary" })
@Html.Label("SplcodeRegSummary", "Special codewise Registration summary")<br />
@Html.RadioButtonFor(model => model.ReportName, "SplcodeReg", new { id = "SplcodeReg" })
@Html.Label("SplcodeReg", "Special codewise Registrations")<br />
</TD>
</TR>
<TR>
<TD>
@Html.Partial("~/Views/Shared/ReportFilters.cshtml")
</TD>
</TR>
</TABLE>
局部视图:
@model MVCProject.Models.ReportFilters
@using (Html.BeginForm("MISPopup", "MIS", FormMethod.Get, new { target = "_blank" }))
{
<div >
<table width="100%">
<tr>
<td>
@Html.HiddenFor(model => model.ReportName)
@Html.Label("ClinicBranch", "Clinic Branch")
</td>
<td colspan="3">
@try
{
@Html.DropDownListFor(model => model.BranchCode, new SelectList(ViewBag.Branch, "BranchCode", "BranchName"), "--Select Branch--")
}
catch (Exception e)
{
@Html.DropDownList("Branch", new SelectList(string.Empty, "Value", "Text"), "--Select Branch--")
}
@Html.ValidationMessageFor(model => model.BranchCode)
</td>
</tr>
<tr>
<td>
@Html.Label("FromDate", "From date")
</td>
<td>
@Html.EditorFor(model => model.PeriodFrom)
@Html.ValidationMessageFor(model => model.PeriodFrom)
</td>
<td>
@Html.Label("ToDate", "To date")
</td>
<td>
@Html.EditorFor(model => model.PeriodTo)
@Html.ValidationMessageFor(model => model.PeriodTo)
</td>
<tr><td colspan="4"><br /></td></tr>
<tr>
<td colspan="4" style="text-align:center;">
<input type="image" id="submit" value="View" src="~/Images/View.png" />
</td>
</tr>
</table>
</div>
}
您的单选按钮组不在 <form>
标记内,因此在提交表单时不会发送其值。您需要将单选按钮移动到表单内部(部分)并删除 属性 ReportName
.
的隐藏输入
如果这不可能,您可以考虑处理单选按钮的 click()
事件,因为使用 javascript/jquery 更新表单中的隐藏输入
我有一个视图,它在页面左侧显示一个单选按钮列表,它还在页面右侧加载一个部分视图,其中包含在弹出窗口中显示报告之前要选择的各种过滤器 window.
问题是,当按下提交按钮时,名为 'ReportName' 的 属性 总是 return 一个 Null 值,而不是 return 选定单选按钮的字符串值其他属性,例如 Branchcode、PeriodFrom、PeriodTo return 正确。
what should i do to get proper string value such as AppointmentSummary, SplcodeRegSummary, or SplcodeReg when its radiobutton is selected.
在此先感谢您的帮助。
型号:
public class ReportModel
{
[Required(ErrorMessage = "Please select a Report!")]
public string ReportName { get; set; }
}
public class ReportFilters :ReportModel //inherited
{
[Required(ErrorMessage="Please select Branch name!")]
public int BranchCode { get; set; }
[Required(ErrorMessage = "Please specify 'Period from' date!")]
[DataType(DataType.Date)]
public DateTime PeriodFrom { get; set; }
[Required(ErrorMessage = "Please specify 'Period to' date!")]
[DataType(DataType.Date)]
public DateTime PeriodTo { get; set; }
}
控制器:
public ActionResult MIS1()
{
ViewBag.Branch = (from c in BranchList().AsEnumerable()
orderby c["BranchName"]
select new { BranchCode = c["BranchCode"], BranchName = c["BranchName"] }).ToList();
return PartialView();
}
public ActionResult MISPopup(ReportFilters rf)
{
if (ModelState.IsValid)
{
return View(rf);
}
return View();
}
查看:
<TABLE>
<TR>
<td>
@Html.RadioButtonFor(model => model.ReportName, "AppointmentSummary", new { id = "AppointmentSummary" })
@Html.Label("AppointmentSummary", "Appointment Summary")<br />
@Html.RadioButtonFor(model => model.ReportName, "SplcodeRegSummary", new { id = "SplcodeRegSummary" })
@Html.Label("SplcodeRegSummary", "Special codewise Registration summary")<br />
@Html.RadioButtonFor(model => model.ReportName, "SplcodeReg", new { id = "SplcodeReg" })
@Html.Label("SplcodeReg", "Special codewise Registrations")<br />
</TD>
</TR>
<TR>
<TD>
@Html.Partial("~/Views/Shared/ReportFilters.cshtml")
</TD>
</TR>
</TABLE>
局部视图:
@model MVCProject.Models.ReportFilters
@using (Html.BeginForm("MISPopup", "MIS", FormMethod.Get, new { target = "_blank" }))
{
<div >
<table width="100%">
<tr>
<td>
@Html.HiddenFor(model => model.ReportName)
@Html.Label("ClinicBranch", "Clinic Branch")
</td>
<td colspan="3">
@try
{
@Html.DropDownListFor(model => model.BranchCode, new SelectList(ViewBag.Branch, "BranchCode", "BranchName"), "--Select Branch--")
}
catch (Exception e)
{
@Html.DropDownList("Branch", new SelectList(string.Empty, "Value", "Text"), "--Select Branch--")
}
@Html.ValidationMessageFor(model => model.BranchCode)
</td>
</tr>
<tr>
<td>
@Html.Label("FromDate", "From date")
</td>
<td>
@Html.EditorFor(model => model.PeriodFrom)
@Html.ValidationMessageFor(model => model.PeriodFrom)
</td>
<td>
@Html.Label("ToDate", "To date")
</td>
<td>
@Html.EditorFor(model => model.PeriodTo)
@Html.ValidationMessageFor(model => model.PeriodTo)
</td>
<tr><td colspan="4"><br /></td></tr>
<tr>
<td colspan="4" style="text-align:center;">
<input type="image" id="submit" value="View" src="~/Images/View.png" />
</td>
</tr>
</table>
</div>
}
您的单选按钮组不在 <form>
标记内,因此在提交表单时不会发送其值。您需要将单选按钮移动到表单内部(部分)并删除 属性 ReportName
.
如果这不可能,您可以考虑处理单选按钮的 click()
事件,因为使用 javascript/jquery 更新表单中的隐藏输入