簡體   English   中英

ASP.NET-MVC-從另一個模型獲取數據

[英]ASP.NET - MVC - Getting data back from another Model

目前我有這個:

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的新手。 仍然靠我自己學習。

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

在您的EvenementorEvent類中添加Type屬性,以便將其綁定到該類,

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" })

根據您的要求進行更改。 希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM