我是MVC的新手。我希望得到" fromDate"和#34; ToDate"从一个角度来看。在同一视图中,我需要三个空的隐藏的DropDownLists。
点击提交按钮后,这些DropDownLists应该是可见的,并根据所选日期填充数据。
但是我得到的是空参考'视图页面上出错。
我的观看页面
<h2>View Data in Database</h2>
<table><tr><td>
@using(Html.BeginForm("ViewPage1","Home"))
{
<table><tr><td>From Date:</td>
<td>@html.TextBoxFor(m=>m.FromDate,"{0:dd/MM/yyyy}")</td>
</tr>
<tr><td>To Date:</td>
<td>@html.TextBoxFor(m=>m.ToDate,"{0:dd/MM/yyyy}")</td>
</tr>
<tr><td><input type="submit" Value="Show"></td>
<td><input type="submit" Value="Show"></td>
</tr>
</table>
<div id="ShowDropBoxes">
<table>
<tr><td>@Html.CheckBox("Production Order:", new{id="ck1"})</td>
<td>@Html.DropDownlistFor(m=>m.ProdNo, Model.ProdOrdList, "Select Production Order")</td>
</tr>
<tr><td>@Html.CheckBox("Part Number:", new{id="ck2"})</td>
<td>@Html.DropDownlistFor(m=>m.PartNo, Model.PartNoList, "Select Part Number")</td>
</tr>
<tr><td>@Html.CheckBox("Status:", new{id="ck3"})</td>
<td>@Html.DropDownlistFor(m=>m.StatusTxt, Model.StatusList, "Select Status")</td>
</tr>
</table>
</div>
我的模型
public class HomeModel
{
public DateTime FromDate {get; set; }
public DateTime ToDate {get; set; }
public string ProdNo {get; set; }
public string PartNo {get; set; }
public int status {get; set; }
public System.Web.Mvc.SelectList ProdNoList {get; set; }
public System.Web.Mvc.SelectList PartNoList {get; set; }
public System.Web.Mvc.SelectList StatusList {get; set; }
}
控制器是: -
public class HomeController: Controller
{
Repository List_in_Repository = new Repository();
public ActionResult ViewPage1()
{
return View();
}
[HttpPost]
public ActionResult ViewPage(HomeModel model)
{
string fromdate = model.FromDate.Tostring("yyyyMMdd");
string todate = model.ToDate.Tostring("yyyyMMdd");
model.ProdNoList = new SelectList(List_in_Repository.GetProductionOrders(fromdate,todate));
model.PartNoList= new SelectList(List_in_Repository.GetPartNumbers(fromdate,todate));
model.StatusList = new SelectList(List_in_Repository.GetStatus(fromdate,todate));
return View(model);
}
}
答案 0 :(得分:0)
您的GET方法不会为属性$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl'] = array (
'_DEFAULT' => array (
'init' => array (
'enableCHashCache' => '1',
'appendMissingSlash' => 'ifNotFile',
'enableUrlDecodeCache' => '1',
'enableUrlEncodeCache' => '1',
),
'redirects' => array (
),
'preVars' => array (
'0' => array (
'GETvar' => 'no_cache',
'valueMap' => array (
'nc' => '1',
),
'noMatch' => 'bypass'
),
'1' => array (
'GETvar' => 'L',
'valueMap' => array (
'de' => '0',
'en' => '1',
'cs' => '2'
),
'noMatch' => 'bypass'
),
'2' => array (
'GETvar' => 'lang',
'valueMap' => array (
'de' => 'de',
),
'noMatch' => 'bypass',
),
),
'pagePath' => array (
'type' => 'user',
'userFunc' => 'EXT:realurl/class.tx_realurl_advanced.php:&tx_realurl_advanced->main',
'spaceCharacter' => '-',
'languageGetVar' => 'L',
'expireDays' => '7',
),
'fixedPostVars' => array (
'newsDetailConfiguration' => array(
array(
'GETvar' => 'tx_news_pi1[action]',
'valueMap' => array(
'detail' => '',
),
'noMatch' => 'bypass'
),
array(
'GETvar' => 'tx_news_pi1[controller]',
'valueMap' => array(
'Legamaster' => '',
),
'noMatch' => 'bypass'
),
array(
'GETvar' => 'tx_news_pi1[news]',
'lookUpTable' => array(
'table' => 'tx_news_domain_model_news',
'id_field' => 'uid',
'alias_field' => 'title',
'addWhereClause' => ' AND NOT deleted',
'useUniqueCache' => 1,
'useUniqueCache_conf' => array(
'strtolower' => 1,
'spaceCharacter' => '-'
),
'languageGetVar' => 'L',
'languageExceptionUids' => '',
'languageField' => 'sys_language_uid',
'transOrigPointerField' => 'l10n_parent',
'autoUpdate' => 1,
'expireDays' => 180,
)
)
),
'newsCategoryConfiguration' => array(
array(
'GETvar' => 'tx_news_pi1[overwriteDemand][categories]',
'lookUpTable' => array(
'table' => 'sys_category',
'id_field' => 'uid',
'alias_field' => 'title',
'addWhereClause' => ' AND NOT deleted',
'useUniqueCache' => 1,
'useUniqueCache_conf' => array(
'strtolower' => 1,
'spaceCharacter' => '-'
)
)
)
),
'newsTagConfiguration' => array(
array(
'GETvar' => 'tx_news_pi1[overwriteDemand][tags]',
'lookUpTable' => array(
'table' => 'tx_news_domain_model_tag',
'id_field' => 'uid',
'alias_field' => 'title',
'addWhereClause' => ' AND NOT deleted',
'useUniqueCache' => 1,
'useUniqueCache_conf' => array(
'strtolower' => 1,
'spaceCharacter' => '-'
)
)
)
),
),
'postVarSets' => array (
'_DEFAULT' => array (
'article' => array(
array(
'GETvar' => 'tx_news_pi1[action]',
'noMatch' => 'bypass'
),
array(
'GETvar' => 'tx_news_pi1[controller]',
'noMatch' => 'bypass'
),
array(
'GETvar' => 'tx_news_pi1[year]',
),
,SelectList
和ProdNoList
初始化PartNoList
,因此当您尝试在StatusList
中访问它们时抛出异常。
你GET方法需要使用空列表添加初始值(例如使用@Html.DropDownlistFor(m => m.ProdNo, Model.ProdOrdList ...)
)
当您最初显示视图时,您不清楚如何“隐藏”下拉列表,但您应该考虑在POST方法中设置为Enumerable.Empty<T>()
的视图模型属性(bool DisplayDropDowns
)在返回视图之前。然后你可以使用
true
这意味着@if(Model.DisplayDropDowns)
{
.... // render dropdowns
}
不需要在GET方法中初始化,因为SelectList
块中的代码永远不会被执行。
另请注意,POST方法应包含if