Search code examples
asp.net-mvcmodelenumsdropdownlistfor

How to create Static Dropdownlist values in MVC Model


How can I prepare a model for Dropdownlist static values (not retrieved from database) like enum or list in MVC Model so that it could be used many times in a project? I would appreciate if you can give a good article? Thanks.


Solution

  • Finally I have found the solution by describing the parameters as hidden input. I am not sure if there is a more elegant method in order to pass the parameters from View to Controller. Thank you so much for your good sample. I also marked as helpful all of your replies. Here is my final code for those who might encounter a similar problem:


    ApplicantViewModel:

    public class ApplicantViewModel
    {
        public IEnumerable<Applicant> Applicants { get; set; }
    
        //Codes for Dropdownlist values
        public string SelectedValue { get; set; }
        public IEnumerable<SelectListItem> Values
        {
            get
            {
                return new[]
                {
                    new SelectListItem { Value = "pdf", Text = "Pdf" },
                    new SelectListItem { Value = "excel", Text = "Excel" },
                    new SelectListItem { Value = "word", Text = "Word" }
                };
            }
        }
    }
    


    ApplicantController:

    public ViewResult Reporting()
    {
        var model = new ApplicantViewModel(); 
        return View(model);
    }
    
    public ActionResult RenderReport(string SelectedValue, string name, string fileName, string dataSource, string table, string filter)
    {
        //Codes for rendering report
        ...
    }
    


    Reporting.cshtml:

    @model MyProject.Models.ApplicantViewModel
    
    @using (Html.BeginForm("RenderReport", "Applicant", FormMethod.Post,
    new { enctype = "multipart/form-data" }))
    { 
     <div>
        @Html.DropDownListFor(model => model.SelectedValue, Model.Values, "-- select an option --")
    
        <input type="hidden" name="name" value="Report1"/>
        <input type="hidden" name="fileName" value="image rapor"/>
        <input type="hidden" name="dataSource" value="ApplicantDataset"/>
        <input type="hidden" name="table" value="ApplicantsView"/>
        <input type="hidden" name="filter" value="David"/>
    
        <input type="submit" value="submit" />
     </div>  
    }