2014-09-02 91 views
-1
獲取數據回

目前我有這樣的:ASP.NET - MVC - 從另一個型號

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新手。仍在自學。

+0

您可能想了解如何使用[AuthoriseAttribute](http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute(v = vs.118).aspx) – 2014-09-02 23:38:55

+0

我會!謝謝! – user3581249 2014-09-02 23:49:20

回答

1

您有提供的代碼有2個選項。

類型屬性添加到您的EvenementorEvent類,所以它會與它結合,

public class EvenmentorEvent { 
    public int Type { get;set; } 
} 

或..添加參數到你的動作:下面

public ActionResult AddEvent(EvenementorEvent ev, int type) 

的人們把更多的努力進入他們的答案,並確實給你更正確的「MVC」方式來做事情。如果你有時間去接受他們的例子並加以實施,他們可能會爲你更好地工作。取決於您的要求。

+0

這兩個選項都會給出錯誤:對象引用未設置爲對象的實例。在我的觀點的foreach。 – user3581249 2014-09-02 23:37:05

+0

什麼錯誤?你能告訴我這個錯誤嗎? – 2014-09-02 23:38:28

+0

是的,因爲這︰ViewData [「Types」] = EvenementorType.GetAllTypes();需要在你的//出錯之前添加!返回View(ev); – 2014-09-02 23:39:33

1

ViewData僅用於單向行程(控制器 - >查看)。如果您想將選定的值從下拉列表傳遞到控制器,則應在模型中創建一個新屬性,並在視圖中強制綁定它。

型號 - >

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 
    } 

查看 - >

​​

所做的更改會按您的要求。希望能幫助到你。