与ASP MVC + Html.DropDownList)使用模型视图模式问题( [英] problems with ASP MVC + Html.DropDownList() using a ModelView Pattern
问题描述
最近,我发布了关于HTML帮助DropDownList的问题,并得到它的工作(<一个href=\"http://stackoverflow.com/questions/1037337/retrieving-data-from-html-dropdownlist-in-controller-asp-mvc-string-returne\">here).但现在我已经决定这是很多聪明切换到模型视图模式,所以我必须查看在强类型在我的意见等。我所做的就是我在通过以下方式等话题做了一些调整,code方法:
VacatureFormViewModel:
公共类VacaturesFormViewModel
{
公共Vacatures Vacature {搞定;私人集; }
公众的SelectList EducationLevels {搞定;私人集; }
公众的SelectList分行{搞定;私人集; }
公众的SelectList CareerLevels {搞定;私人集; } 资源库资源库; //构造
公共VacaturesFormViewModel(Vacatures vacature)
{
this.Vacature = vacature;
this.repository =新的存储库();
this.EducationLevels =新的SelectList(repository.GetAllEducationLevels(),ID,姓名,vacature.EducationLevels);
this.Branches =新的SelectList(repository.GetAllBranches(),ID,姓名,vacature.Branches);
this.CareerLevels =新的SelectList(repository.GetAllCareerLevels(),ID,姓名,vacature.CareerLevels); }
}
BanenController:
//
// GET:/ Banen /创建 公众的ActionResult的Create()
{
Vacatures vacature =新Vacatures();
返回查看(新VacaturesFormViewModel(vacature));
} //
// POST:/ Banen /创建 的[AcceptVerbs(HttpVerbs.Post)
公众的ActionResult创建(Vacatures vacatureToAdd)
{
如果(ModelState.IsValid)
{
尝试
{
// TODO:添加插入逻辑这里
repository.AddToVacatures(vacatureToAdd);
repository.SaveChanges(); //返回如果成功的上市页
返回RedirectToAction(「指数」);
}
赶上(例外五)
{
返回查看();
}
}
}
和我Create.aspx视图(它的一部分):
&LT;使用%(Html.BeginForm()){%GT; &LT;&字段集GT;
&LT;传奇&GT;&领域LT; /传说&GT;
&所述p为H.;
&LT;标签=标题&GT;标题:LT; /标签&gt;
&LT;%= Html.TextBox(标题,Model.Vacature.Title)%GT;
&LT;%= Html.ValidationMessage(标题,*)%GT;
&所述; / P&GT;
&所述p为H.;
&LT;标签=内容&gt;内容:LT; /标签&gt;
&LT;%= Html.TextArea(内容,Model.Vacature.Content)%GT;
&所述;%= Html.ValidationMessage(内容,*)%&GT;
&所述; / P&GT;
&所述p为H.;
&LT;标签=EducationLevels&GT; EducationLevels:LT; /标签&gt;
&所述;%= Html.DropDownList(EducationLevels,Model.EducationLevels)%GT;
&所述;%= Html.ValidationMessage(EducationLevels,*)%&GT;
&所述; / P&GT;
&所述p为H.;
&LT;标签=CareerLevels&GT; CareerLevels:LT; /标签&gt;
&所述;%= Html.DropDownList(CareerLevels,Model.CareerLevels)%GT;
&所述;%= Html.ValidationMessage(CareerLevels,*)%&GT;
&所述; / P&GT;
&所述p为H.;
&LT;标签=分行&GT;分行:其中; /标签&gt;
&LT;%= Html.DropDownList(分支,Model.Branches)%GT;
&LT;%= Html.ValidationMessage(分行,*)%GT;
&所述; / P&GT;
&所述p为H.;
&LT;输入类型=提交值=保存/&GT;
&所述; / P&GT;
&LT; /字段集&GT;
&LT;%}%GT;
有关指导我已经ScottGu\">的NerdDinner <使用的NerdDinner教程的按ScottGu的教程,我看过这里的各种话题。
我的问题是,如果有可能让MVC ASP设置我careerlevel,educationlevel和领域(dropdownl
编辑使用GUID:
使用dropdownlists我使用略有不同的方法。这也许可以让您的视图模型的工作,但对我来说很容易通过这种方式:
公共类VacaturesFormViewModel
{ 公共IEnumerable的&LT; SelectListItem&GT; EducationLevels {搞定;组; }
公众的Guid EducationLevelID {搞定;组; }}
该EducationLevelID将有你的下拉的选择ID。
这是视图:
&LT;%= Html.DropDownList(EducationLevelID,Model.EducationLevels)%GT;
控制器
的IEnumerable&LT; SelectListItem&GT; educationLevelList =
从GetLevelList水平()
选择新SelectListItem
{
文本=水平.name和
值= level.Uid.ToString()
};
model.EducationLevels = educationLevelList;
Recently I posted a question about the html helper dropdownlist and got it working (here). But now I have decided it was alot smarter to switch to ModelView Patterns so I have acces to strongly typed methods in my views etc. What I did was I made some adjustments to the code in my other topic in the following way:
VacatureFormViewModel:
public class VacaturesFormViewModel
{
public Vacatures Vacature { get; private set; }
public SelectList EducationLevels { get; private set; }
public SelectList Branches { get; private set; }
public SelectList CareerLevels { get; private set; }
Repository repository;
// Constructor
public VacaturesFormViewModel(Vacatures vacature)
{
this.Vacature = vacature;
this.repository = new Repository();
this.EducationLevels = new SelectList(repository.GetAllEducationLevels(),"ID","Name",vacature.EducationLevels);
this.Branches = new SelectList(repository.GetAllBranches(),"ID","Name",vacature.Branches);
this.CareerLevels = new SelectList(repository.GetAllCareerLevels(), "ID", "Name", vacature.CareerLevels);
}
}
BanenController:
//
// GET: /Banen/Create
public ActionResult Create()
{
Vacatures vacature = new Vacatures();
return View(new VacaturesFormViewModel(vacature));
}
//
// POST: /Banen/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Vacatures vacatureToAdd)
{
if (ModelState.IsValid)
{
try
{
// TODO: Add insert logic here
repository.AddToVacatures(vacatureToAdd);
repository.SaveChanges();
// Return to listing page if succesful
return RedirectToAction("Index");
}
catch (Exception e)
{
return View();
}
}
}
And my Create.aspx view (part of it):
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="Title">Title:</label>
<%= Html.TextBox("Title", Model.Vacature.Title) %>
<%= Html.ValidationMessage("Title", "*") %>
</p>
<p>
<label for="Content">Content:</label>
<%= Html.TextArea("Content", Model.Vacature.Content) %>
<%= Html.ValidationMessage("Content", "*") %>
</p>
<p>
<label for="EducationLevels">EducationLevels:</label>
<%= Html.DropDownList("EducationLevels", Model.EducationLevels)%>
<%= Html.ValidationMessage("EducationLevels", "*") %>
</p>
<p>
<label for="CareerLevels">CareerLevels:</label>
<%= Html.DropDownList("CareerLevels", Model.CareerLevels)%>
<%= Html.ValidationMessage("CareerLevels", "*")%>
</p>
<p>
<label for="Branches">Branches:</label>
<%= Html.DropDownList("Branches", Model.Branches)%>
<%= Html.ValidationMessage("Branches", "*")%>
</p>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
For guiding I have used the NerdDinner tutorial by ScottGu and I have read various topics here.
My question is if it is possible to let MVC ASP set my careerlevel, educationlevel and branche (dropdownl
Edited to use Guid:
With dropdownlists I use a slightly different approach. It might be possible to get your viewmodel working, but for me it is easier this way:
public class VacaturesFormViewModel
{
public IEnumerable<SelectListItem>EducationLevels{ get; set; }
public Guid EducationLevelID{ get; set; }
}
The EducationLevelID will have the selected id of your Dropdown. This is the view:
<%= Html.DropDownList("EducationLevelID", Model.EducationLevels)%>
Controller
IEnumerable<SelectListItem> educationLevelList =
from level in GetLevelList()
select new SelectListItem
{
Text = level .Name,
Value = level.Uid.ToString()
};
model.EducationLevels = educationLevelList ;
这篇关于与ASP MVC + Html.DropDownList)使用模型视图模式问题(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!