我需要选择Quarter and Year
然后提交这两个值应传递给我的存储过程"SummaryProc"
并在执行后将传递ID的行.....请指导我,如果我写的代码很好。我错过了链接,因为我无法理解如何链接所有这些。工作应用将是"Enter Quarter & Year in _Summary.cshtml -> Pass values to Controller -> Execution in Repository code -> rows returned from stored proc to be displayed in SummaryTable.cshtml"
_Summary.cshtml
<div class="form-group">
<label class="control-label col-lg-4">Period:</label>
<div class="col-lg-8">
@Html.DropDownList("Quarter", new SelectListItem[] { (new SelectListItem() { Text = "Q1", Value = "1" }), (new SelectListItem() { Text = "Q2", Value = "2" }), (new SelectListItem() { Text = "Q3", Value = "3" }), (new SelectListItem() { Text = "Q4", Value = "4" }) }, "--Select Quarter--", new { @class = "form-control", id = "Quarter" })
<br />
@Html.DropDownList("Year", new SelectListItem[] { (new SelectListItem() { Text = "2016", Value = "2016" }), (new SelectListItem() { Text = "2017", Value = "2017" }) }, "--Select Year--", new { @class = "form-control", id = "Year" })
</div>
</div>
<div class="row">
<div class="col-lg-offset-4" style="padding: 10px 0px 0px 0px;">
<input type="submit" id="submit" value="Submit" class="btn btn-lg btn-success" />
</div>
</div>
控制器代码:
[ActionName("_Summary")]
[HttpPost]
public ActionResult _Summary(FloorPlanViewModel model, FormCollection form)
{
SummaryConfiguration sumConfig = new SummaryConfiguration();
sumConfig.Quarter = Convert.ToInt32(form["Quarter"]);
sumConfig.Year = Convert.ToInt32(form["Year"]);
var details = floorService.SummaryProc(sumConfig.Quarter, sumConfig.Year);
return PartialView("SummaryTable", details);
}
执行存储过程的代码:
public IEnumerable<SummaryConfiguration> SummaryProc(int Quarter, int Year)
{
var model = new List<SummaryConfiguration>();
using (var dbCmd = defaultDB.GetStoredProcCommand("dbo.SummaryProc"))
{
defaultDB.AddInParameter(dbCmd, "Quarter", System.Data.DbType.Int32, Quarter);
defaultDB.AddInParameter(dbCmd, "Year", System.Data.DbType.Int32, Year);
var varCmd = defaultDB.ExecuteReader(dbCmd);
while (varCmd.Read())
{
// add items in the list
model.Add(new SummaryConfiguration()
{
ID = Convert.ToString(varCmd["ID"]),
//rest of the code
});
}
}
return model;
}
答案 0 :(得分:1)
在不使用ajax的情况下处理此问题的典型方法是使用单个GET方法和视图回发到该方法。
首先创建一个视图模型来表示您的视图
public class SummaryConfigurationVM
{
public int Quarter { get; set; }
public int Year { get; set; }
public IEnumerable<SummaryConfiguration> Configurations { get; set; }
public IEnuemrable<SelectListItem> QuarterList { get; set; }
public IEnuemrable<SelectListItem> YearListList { get; set; }
}
并添加可能适用于Quarter
和Year
的任何验证属性(例如[Range]
属性)
,你的控制器方法将是
public ActionResult Summary(int? quarter, int? year)
{
IEnumerable<SummaryConfiguration> configurations = new List<SummaryConfiguration>();
if (quarter.HasValue && year.HasValue)
{
configurations = floorService.SummaryProc(quarter.Value, year.Value);
}
SummaryConfigurationVM model = new SummaryConfigurationVM()
{
Quarter = quarter,
Year = year,
Configurations = configurations,
QuarterList = new List<SelectListItem>()
{
new SelectListItem(){ Text = "Q1", Value = "1" },
....
},
YearList = new List<SelectListItem>()
{
....
}
};
return View(model);
}
请注意,您还需要考虑验证quarter
和year
的值是否在预期范围内。
您的观点将是
@model SummaryConfigurationVM
....
@using (Html.BeginForm("Summary", "yourControllerName", FormMethod.Get))
{
@Html.LabelFor(m => m.Quarter)
@Html.DropDownListFor(m => m.Quarter, Model.QuarterList, "--Select Quarter--")
@Html.ValidationMessageFor(m => m.Quarter)
.... // ditto for Year
<input type="submit" value="Submit" />
}
<table>
<thead>
<tr>
<th>@Html.DisplayNameFor(m => m.Configurations.FirstOrDefault().LineID)</th>
....
</tr>
</thead>
<tbody>
@foreach( var item in Model.Configurations)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.LineID)</td>
.... // other properties of SummaryConfiguration
</tr>
}
</tbody>
</table>
您可能还会考虑在<table>
块旁边呈现@if (Model.Configuraton.Any()) {
(如果false
,则会显示一条消息,指出未找到任何结果。