ASP.NET-MVC-从另一个模型中获取数据
本文关键字:获取 数据 模型 另一个 NET-MVC- ASP | 更新日期: 2023-09-27 18:20:29
目前我有这个:
AddEvent.cshtml
@model Evenementor.Models.EvenementorEvent
@{
ViewBag.Title = "Voeg een evenement toe";
}
@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl }))
{
<div class="12u">
@Html.TextBoxFor(m => m.Title, new { @placeholder = "Titel" })
</div>
<div class="12u">
@Html.TextAreaFor(m => m.Description, new { @placeholder = "Omschrijving" })
</div>
<div class="6u">
@Html.TextBoxFor(m => m.Startdate, new { @placeholder = "Startdag" })
</div>
<div class="6u">
@Html.TextBoxFor(m => m.Enddate, new { @placeholder = "Einddag" })
</div>
<div class="12u">
@Html.TextBoxFor(m => m.Location, new { @placeholder = "Locatie" })
</div>
<div class="12u">
<select name="Type">
@foreach (var item in ViewData["Types"] as List<Evenementor.Models.EvenementorType>)
{
<option value="@item.TypeID">@item.Name</option>
}
</select>
</div>
<input type="submit" value="Voeg toe" />
}
DashboardController.cs
// GET: /Dashboard/AddEvent
public ActionResult AddEvent()
{
if (!User.Identity.IsAuthenticated)
{
// User isn't allowed here
Response.Redirect("/");
}
ViewData["Types"] = EvenementorType.GetAllTypes();
return View();
}
// POST: /Dashboard/AddEvent
[HttpPost]
public ActionResult AddEvent(EvenementorEvent ev)
{
if (!User.Identity.IsAuthenticated)
{
// User isn't allowed here
Response.Redirect("/");
}
if (ModelState.IsValid)
{
EvenementorEvent e = new EvenementorEvent();
e.Title = ev.Title;
e.Description = ev.Description;
e.Startdate = ev.Startdate;
e.Enddate = ev.Enddate;
e.Location = ev.Location;
// Register the event
e.Register(e);
// Make a link between event and organisation
EvenementorOrganisationsEvent l = new EvenementorOrganisationsEvent();
l.EventID = e.EventID;
l.OrganisationID = EvenementorOrganisation.GetOrganisationByEmail(User.Identity.Name).OrganisationID;
l.Register(l);
// Link between event and type
EvenementorEventsType ty = new EvenementorEventsType();
ty.EventID = e.EventID;
//GET THE OTHER INFO (Types)
// Redirect
Response.Redirect("/Dashboard/");
}
// Something's wrong!
return View(ev);
}
如何将下拉列表中的输入数据返回到我的控制器中?或者还有其他更好的解决方案吗?
提前感谢,我是ASP.NET的新手。我还在自学。
您有两个选项和提供的代码。
将Type属性添加到您的EvenimentorEvent类中,这样它就会绑定到它,
public class EvenmentorEvent {
public int Type { get;set; }
}
或者。。为您的操作添加一个参数:
public ActionResult AddEvent(EvenementorEvent ev, int type)
下面的人在他们的答案上付出了更多的努力,确实给了你更正确的"MVC"做事方式。如果你有时间学习并实施他们的例子,他们可能会对你更好。取决于您的要求。
ViewData仅用于单程跳闸(控制器->View)。如果要将下拉列表中的选定值传递给控制器,则应在模型中创建一个新属性,并在视图中对其进行强绑定。
型号->
public class EvenementorEvent
{
public string Title { get;set;}
public string Description { get;set;}
....
....
public IEnumerable<System.Web.Mvc.SelectListItem> ListOfData { get;set;}
public string SelectedData { get;set;}
}
控制器->
public ActionResult Index()
{
EvenementorEvent ev = new EvenementorEvent();
.............
.............
ev.ListOfData = ( Populate Data - Transform your collection to Collection of
System.Web.Mvc.SelectListItem)
return View(ev);
}
[HttpPost]
public ActionResult AddEvent(EvenementorEvent ev)
{
ev.SelectedData -> The selected value from the dropdown
}
查看->
@Html.DropDownListFor(m => m.SelectedData, Model.ListOfData , new { id = "dropdown" })
根据您的要求进行更改。希望能有所帮助。