我有一些代码,其中第二个有效,第一个无效。这个想法是用户选择了一个站点,然后它显示该站点的信息,选择一个站点自动提交表单。
注意:我不需要第二个,我只是想弄清楚为什么前者不起作用
视图模型
public class SitesViewModel
{
public SitesViewModel()
{
int allowMaintainSites = SessionWrapper.AllowMaintainSites.HasValue ? SessionWrapper.AllowMaintainSites.Value : 0;
this.isAllowedMaintain = allowMaintainSites == 1;
}
public SelectList companySelectList { get; set; }
public SelectList siteSelectList { get; set; }
public SelectList siteSelectList1 { get; set; }
public SelectList siteTypeSelectList { get; set; }
public bool isAllowedMaintain { get; set; }
public bool isAllowedSuper { get; set; }
public bool companySelected { get; set; }
public bool siteSelected { get; set; }
public bool hasSites { get; set; }
public int? currentSite { get; set; }
public int? companyId { get; set; }
public Site site { get; set; }
public Site site1 { get; set; }
}
控制器:
[HttpGet()]
public ActionResult Sites()
{
var viewModel = new SitesViewModel();
viewModel.companySelectList = viewModel.isAllowedMaintain ?
new SelectList(context.GetCompaniesAll(), "companyId", "name") :
new SelectList(context.GetCompaniesByUser(SessionWrapper.UserId.Value), "companyId", "name");
return View(viewModel);
}
[HttpPost()]
public ActionResult Sites(string data)
{
var viewModel = new SitesViewModel();
if (Request["company"] != null)
{
int? companyId = stringToNullInt(Request["company"]);
viewModel.companySelected = true;
viewModel.companyId = companyId;
viewModel.companySelectList = viewModel.isAllowedMaintain ?
new SelectList(context.GetCompaniesAll(), "companyId", "name",companyId) :
new SelectList(context.GetCompaniesByUser(SessionWrapper.UserId.Value), "companyId", "name",companyId);
viewModel.siteSelectList = new SelectList(context.GetSitesByCompany(companyId,false),"siteId","name") ;
viewModel.siteSelectList1 = new SelectList(context.GetSitesByCompany(companyId, false), "siteId", "name");
viewModel.hasSites = context.GetSitesByCompany(companyId, false).Count() > 0;
if(!Request["site"] == null)
{
int? siteId = stringToNullInt(Request["site"]);
viewModel.site = context.GetSiteById(siteId).FirstOrDefault();
viewModel.siteSelectList = new SelectList(context.GetSitesByCompany(companyId, false), "siteId", "name", viewModel.site.siteId.Value);
}
if(!Request["site1"] == null){
int? siteId = stringToNullInt(Request["site1"]);
viewModel.site1 = context.GetSiteById(siteId).FirstOrDefault();
viewModel.siteSelectList1 = new SelectList(context.GetSitesByCompany(companyId, false), "siteId", "name", viewModel.site1.siteId.Value);
}
}
}
看法
@using (Html.BeginForm())
{
@Html.ValidationSummary(false, "Please fix these errors.");
<div id="CustomerSelect">
<div class="labels">
<b class="head">Select Customer</b>
</div>
<div class="editors">
@Html.DropDownList("company", @Model.companySelectList, "Please Select Customer", new Dictionary<string, object> { { "class", "selectList" }, { "onchange", "submit()" } })
</div>
</div>
if (Model.companySelected)
{
if (!Model.hasSites)
{
<div class="result">
<p>
<b class="head">There are no sites found.</b>
</p>
</div>
}
else
{
<div id="SiteSelect">
<p class="labels">
<b class="head">Select Site</b>
</p>
<p class="editors">
@Html.DropDownList("site",@Model.siteSelectList,"Please Select Site",new Dictionary<string,object>{{"class","selectList"},{"onchange","submit()"}})
@Html.DropDownList("site1",@Model.siteSelectList1,"Please Select Site",new Dictionary<string,object>{{"class","selectList"},{"onchange","submit()"}})
</p>
</div>
}
}
}
现在,当我选择前一个“站点”时,它具有正确的选定值(当我在控制器中调试时)并且正确的项目属性设置为 true。但是,每次页面加载时,它都会选择“请选择站点”,但是当我更改第二个(“site1”)时,一切似乎都正常。
有什么建议么?谢谢。
我注意到发生了其他奇怪的事情,如果我更改里面的代码:
if(!Request["site"]==null){
}
使用 viewModel.site 而不是 viewModel.site1 它将起作用......这里发生了一些奇怪的事情
编辑:已解决
解决方法是将所有内容更改为不同的名称。我想,当我打电话时
Html.DropDownList("site",....)
它试图将它与我的 SitesViewModel 中的属性站点相匹配,这不是我想要的!我目前已将其重命名为“site1”,一切似乎都正常工作,这解释了为什么在上面的代码中,它根本不起作用,然后当我根据 Secret Squirrel 的回答添加了 site1 属性时,
Html.DropDownList("site1",...) didn't work either!
我希望这对将来的某人有所帮助,因为上帝需要很长时间才能解决!