我有一个链接到模型的存储过程,并且在提交时,我试图将Value设为基于下拉列表的参数。将字符串放入参数中时,可以得到最终值。但是,我希望基于使用提交按钮进行选择的下拉列表的最终价值。请帮助我如何将下拉列表中的选定值传递给参数。我的参数是GetWACC()@ oplocation,@ Industry,@ Date)。我尝试将列表传递给参数并转换为ToString(),但是它不起作用。请帮忙
非常感谢。
这是我的HomeController:
public ActionResult WACC()
{
ViewBag.Message = "Your WACC page.";
//Set Industry : Later make it DB-Driven
//List<SelectListItem> lstIndustry = new List<SelectListItem>() {
// new SelectListItem {Text = "Financials", Value = "1"},
// new SelectListItem {Text = "Real Estate", Value = "2"},
//};
// Pre-set values
var objWACC = new WACC();
objWACC.ValuationDate = new DateTime(2018, 11, 20);
//objWACC.IndustryList = lstIndustry;
objWACC.IndustryList = WACC_Lib.GetIndustry();
//objWACC.IndustrySelected = 1;
objWACC.LocationList = WACC_Lib.GetOpLocations();
//objWACC.LocationSelected = 1;
DateTime waccdate = objWACC.ValuationDate;
string indlist = objWACC.IndustryList.ToString();
string loclist = objWACC.LocationList.ToString();
objWACC.FairValue = WACC_Lib.GetWACC(loclist, indlist, waccdate);
// Redirect to WACC page
return View(objWACC);
}
这是我的WACC控制器:
public class WACCController : ApiController
{
[HttpGet]
[Route("GetWACC")] // for testing, F5 and change the link to http://localhost:54192/API/WACC/GetWACC?strOpLocation=Hong%20%Kong&strIndustry=Financials&strIniDate=2017-12-31&strValDate=2018-12-31 , the port no may be different (no "HOME")
public double GetWACC(string strOpLocation, string strIndustry, DateTime ValDate)
{
double dblResult = 0.0;
try
{
dblResult = BDODataLib.WACC_Lib.GetWACC(strOpLocation, strIndustry, ValDate);
}
catch
{
dblResult = -2.0;
}
return dblResult;
}
}
}
这是我的模特:
public class WACC
{
[Key]
[Display(Name = "Industry")]
//public string Industry { get; set; }
public List<SelectListItem> IndustryList { get; set; }
public string IndustrySelected { get; set; }
// public int LocID { get; set; }
[Display(Name = "Location")]
public List<SelectListItem> LocationList { get; set; }
public string LocationSelected { get; set; }
[Display(Name = "Valualtion Date")]
public DateTime ValuationDate { get; set; }
[Display(Name = "WACC")]
public double FairValue { get; set; }
}
这是我的观点:
<table class='wacctable'>
<tr>
<td style="background-color:lightgrey">@Html.DisplayNameFor(x => x.IndustryList)</td>
@*<td>@Html.DropDownList("lstIndustry", (IEnumerable<SelectListItem>)ViewData["lstIndustry"],1)</td>*@
@*<td style="width: 200px">@Html.DropDownListFor(x => x.IndustrySelected, Model.IndustryList, "--Select One--", new {onchange = "document.getElementById('TheForm').submit();" })</td>*@
<td style="width: 200px">@Html.DropDownListFor(x => x.LocID, new SelectList (Model.IndustryList, "Value", "Text"), "--Select One--")</td>
@*<td>@Html.DropDownListFor(model => model.Industry, (IEnumerable<SelectListItem>)ViewData["lstIndustry"])</td>*@
@*<td>@Html.TextBoxFor(model => model.Industry)</td>*@
</tr>
<tr>
<td style="background-color:lightgrey">@Html.DisplayNameFor(x => x.LocationList)</td>
@*<td>@Html.TextBoxFor(x => x.LocationList)</td>*
@*<td>@Html.DropDownListFor(x => x.LocationSelected, Model.LocationList, "--Select One--", new { @class = "abc", onchange = "document.getElementById('TheForm').submit();" })</td>*@
<td>@Html.DropDownListFor(x => x.LocID, new SelectList (Model.LocationList, "Value", "Text"), "--Select One--", new { @class = "abc" })</td>
@*<td>@Html.DropDownListFor(x => Model.LocationSelected, new SelectList(Model.LocationList, "Value"), htmlAttributes: new { @class = "form-control", id = "Location",style = "length: 180px;" })</td>*@
</tr>
<tr>
<td style="background-color:lightgrey">@Html.DisplayNameFor(x => x.ValuationDate)</td>
<td>@Html.TextBoxFor(x => x.ValuationDate, string.Format("{0:d}", Model.ValuationDate.ToString("yyyy-MM-dd")), new { @class = "date-picker", type = "date" })</td>
</tr>
<tr>
<td style="background-color:lightgrey">@Html.DisplayNameFor(m => m.FairValue)</td>
<td>@Html.TextBoxFor(m => m.FairValue, new { @class = "width: 180px;" })</td>
</tr>
</table>
<br />
<input type="submit" value='Submit' />
}
</div>
这是我传递参数时的样子
The parameter return this "System.Collection.Generic.List"
这里的视图看起来像: