mardi 17 novembre 2015

How to bind dropdown list from action method in MVC 3 inside create view

I want to populate a dropdownlist on create view with form also. Here is my code

@model CM.Models.TVSerialEpisode

@{
   ViewBag.Title = "AddEpisode";
}
@Html.Action("ListAllSeason","TvSerial") //here i want to call this action method so i could bind all tv serial

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>TVSerialEpisode</legend>



    <div class="editor-label">
        @Html.LabelFor(model => model.Serial_ID)
    </div>
    <div class="editor-field">

         @Html.DropDownList("Serial_ID", new List<SelectListItem>
        {
           new SelectListItem { Text = "Backstrom", Value="16" },
           new SelectListItem{Text="10 Things I hate about You", Value="15"},
           new SelectListItem { Text = "Castle", Value="14" }
      }, "Select Serial")
         @Html.ValidationMessageFor(model => model.Serial_ID)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Episode_No)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Episode_No)
        @Html.ValidationMessageFor(model => model.Episode_No)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Episode_Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Episode_Name)
        @Html.ValidationMessageFor(model => model.Episode_Name)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.File_Url_480p)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.File_Url_480p)
        @Html.ValidationMessageFor(model => model.File_Url_480p)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.File_Url_720p)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.File_Url_720p)
        @Html.ValidationMessageFor(model => model.File_Url_720p)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Description)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Description)
        @Html.ValidationMessageFor(model => model.Description)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Is_Active)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Is_Active)
        @Html.ValidationMessageFor(model => model.Is_Active)
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

here is the model class

namespace CM.Models
{

[Table("tbltvserial")]
public class TVSerial
{
    [Key]
    public Int64 Serial_ID { get; set; }
    public string Serial_Name { get; set; }
    public string Season_Name { get; set; }
    public int Season_No { get; set; }
    public int? Release_Year { get; set; }       
}
public class TVSerial_VM
{       
    public Int64 Serial_ID { get; set; }
    public string Serial_Name { get; set; }       
    public int Season_No { get; set; }            
}


[Table("tblserialepisode")]
public class TVSerialEpisode
{
    [Key]
    public Int64 Video_ID { get; set; }        
    public Int64 Serial_ID { get; set; }
    public int Episode_No { get; set; }
    public string Episode_Name { get; set; }       
    public string File_Url_480p { get; set; }
    public string File_Url_720p { get; set; }
    public string Description { get; set; }
    public bool Is_Active { get; set; }
    public DateTime Uploaded_Time { get; set; }     
}

public class TvSerialContext : DbContext
{
    public DbSet<TVSerialEpisode> TvSerialEpisodes { get; set; }
    public DbSet<TVSerial> TvSerials { get; set; }  
}
}

here are controller class

public class TvSerialController : Controller
{

    public List<TVSerial_VM> ListAllSeason()
    {
        try
        {
            TvSerialContext tvContext = new TvSerialContext();
            List<TVSerial_VM> tv = tvContext.TvSerials
                .Select(t => new TVSerial_VM
                {
                    Serial_Name = t.Serial_Name,
                    Season_No=t.Season_No,
                    Serial_ID=t.Serial_ID
                }).OrderBy(t=> t.Season_Name).
                  ToList();
            return (tv);
        }
        catch (Exception ex)
        {
            return null;
        }

    }
}

public class TvSerialEpisodeController : Controller
{

    TvSerialContext tvContext = new TvSerialContext();
    public ActionResult AddEpisode()
    {
        return View();
    }


    [HttpPost]
    public ActionResult AddEpisode(TVSerialEpisode tvEpisode)
    {
        if (ModelState.IsValid)
        {
            tvContext.TvSerialEpisodes.Add(tvEpisode);
            tvContext.SaveChanges();
            return RedirectToAction("AddEpisode");
        }

        return View(tvEpisode);
    }
}

I don't know how to bind the tvserial list inside above view page. basically i want to choose the serial id from dropdown list, is this posible to call a action method inside the create view.

Aucun commentaire:

Enregistrer un commentaire