MVC получить выбранное значение выпадающего списка в TempData - PullRequest
0 голосов
/ 24 января 2020

Я использую модель MVC и хочу сохранить выбранное значение из выпадающего списка.

вот что у меня есть

Модель.

namespace FormsAuthenticationMVC.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Report_Security
    {
        public int ID { get; set; }
        public Nullable<int> UserName_ID { get; set; }
        public string ReportName { get; set; }
        public string Report_Url { get; set; }
        public string EmbedCode { get; set; }

        public List<Report_Security> ReportList { get; set; }
    }
}

Мой контроллер

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FormsAuthenticationMVC.Models;

namespace FormsAuthenticationMVC.Controllers
{

    //[Authorize]
    public class HomeController : Controller
    {    
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Reports()
        {   
            // this is working fine. the dropdown is populated from the model correctly.

            ViewBag.Message = "O2 Dashboard";

            ASP_AuthEntities ReportTable = new ASP_AuthEntities();

            var getReportTable = ReportTable.Report_Security.ToList();
            SelectList list = new SelectList(getReportTable, "EmbedCode", "ReportName");
            ViewBag.ReportListName = list;

            return View();
    }

        [HttpPost]
        public ActionResult Reports(Report_Security model, string Code)
        {

            if (ModelState.IsValid)

                TempData["HtmlSelected"] = Code;  // = This is where i shall set the dropdown value

            return View(model);        
        }              
    }
}

и мой взгляд

@model FormsAuthenticationMVC.Models.Report_Security     
@{    
    <h2>@ViewBag.Message</h2>
}

@Html.DropDownListFor(r =>r.EmbedCode, new SelectList(Model.EmbedCode,"EmbedCode"),"Select Report")       
<h5>@TempData["HtmlSelected"]</h5>   @*----- SELECTED DROPDOWN VALUE*@ 

Когда я запускаю отладку и ставлю точку останова в моем представлении на @ Html .DropDownListFor .... .. line Я получаю следующую ошибку.

System.Web. Mvc .WebViewPage.Model.get вернул null.

Может кто-нибудь помочь мне, пожалуйста.

...