Click here to Skip to main content
15,896,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In Edit page i have 2 Drop down list and textbox. I used VS 2012 MVC4.0 with Razor view and Entity Framework 5.0
In Edit Mode i can able to view Textbox value and populate Dropdown list .... but Can't Show Selected Value in Dropdown List in Edit view Page (Controller to View).

Here in my Code

Controler :

C#
public ActionResult Edit(long id = 0)
       {
           LIS_MasterLawCategory lis_masterlawcategory = objDB.LIS_MasterLawCategory.Single(l => l.LawCategoryID == id);
           if (lis_masterlawcategory == null)
           {
               return HttpNotFound();
           }
           ViewBag.ParentLawCategoryID = new SelectList(objDB.LIS_MasterLawCategory, "LawCategoryID", "LawCategoryName").OrderBy(l => l.Text);
           ViewBag.CountryID = new SelectList(objDB.LIS_Master_Country, "CountryID", "CountryName").OrderBy(a => a.Text);

           return View(lis_masterlawcategory);
       };



View

C#
@model LISAdmin.LIS_MasterLawCategory
@{    
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/LayoutAdmin.cshtml";
}

<h2>Law Category Edit</h2>
<br clear="all" />
@*Save or Failure Message *@
@if (TempData["returnMsg"] != null)
{
    @Html.Label(TempData["returnMsg"].ToString());
}
@using (Html.BeginForm("Create", "LawCategory", FormMethod.Post, new { @class = "stdform stdform2" }))
{    
    
    <p>
        <label>Category Name</label>
        <span class="field">
            @Html.TextBoxFor(m => Model.LawCategoryName)
            @Html.Hidden("h", (string)ViewBag.Id)
        </span>
    </p>
    <p>
        <label>Short Description <small>You can put your own description for this field here.</small></label>
        <span class="field">
            @Html.TextAreaFor(m => Model.ShortDescription, new { style = "longinput", cols = "50", rows = "4" })
        </span>
    </p>
    <p>
        <label>Parent Law Category</label>
        <span class="field">

            @Html.DropDownListFor(x => x.ParentLawCategoryID ,new SelectList(Model.LawCategoryID,"Value","Text"),"Select one") 


        <label>Country</label>
        <span class="field">
            @Html.DropDownListFor(m => Model.CountryID, (SelectList)ViewData["Country"], "--Select--")

        </span>
    </p>    
    <p>
        <label>Active</label>
        <span class="field">
        @Html.EditorFor(m => Model.IsActive, new { disabled = "disabled" });
    </p>
    <p>
        <label>Menu Display Order</label>
        <span class="field">
            @Html.TextBoxFor(m => Model.MenuDisplayOrder)
        </span>
    </p>

    <p class="stdformbutton">
        <input type="submit" class="submit radius2" value="Submit" />

        <input type="reset" class="reset radius2" value="Reset Button" />
    </p>
}
<br clear="all" />

<div>
    @Html.ActionLink("Back to List", "Index")
</div></blockquote>

<blockquote class="FQ"><div class="FQA">Quote:</div>
Posted
Updated 19-Sep-13 7:41am
v7
Comments
TryAndSucceed 16-Sep-13 12:30pm    
What is the problem you are facing? Please improve your question with your problem and requirement.
TryAndSucceed 18-Sep-13 16:18pm    
Plus, remove the commented code. Its confusing.
SDK03 18-Sep-13 3:54am    
You want to show one value as a selected in dropdown on Edit Page?? Please give some more information!
Jameel VM 18-Sep-13 14:11pm    
your question is not clear.did you want the selected value to controller?

XML
Try below code it works for me by using ViewData ...

Step1: Create class for dropdownlist values
public class Country
{
  public int Id { get; set;}
  public string Text { get; set; }
}

 Step2:; Add following code in Controller
 Controller Code:
 public ActionResult Edit(int id)
 {
  var countryList = new[]
  {
   new Roles{ Text = "India", Id= "1" },
   new Roles{ Text = "USA", Id = "2" },
   new Roles{ Text = "UK", Id = "3"},
   new Roles{ Text = "China", Id = "4"},
   new Roles{ Text = "Japan", Id = "5"},
   new Roles{ Text = "Russia", Id = "6"}
  };
  // Passing selected value to dropdownlist(fetch from database or some where else)...
  // For example your class name is customer, dropdownlist display value will be customer.Country in edit mode
 ViewData["countryList "] = new SelectList(countryList, "Id","Text",customer.Country);
 return View(customer);
}

Step3: Add following code in View
View Code:
@Html.DropDownList("countrtddList", ViewData["countryList"] as SelectList)

It appears that the DropDown must not have the same name has the ViewData name.
Happy coding.....
 
Share this answer
 
As i understand you, you are saying on your edit page you have a select list, and you want to preselect one of the items.

One of my edit views has a dropdown list of departments. here is what is in my controller:
ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "DepartmentName", review.DepartmentID);

review.DepartmentID tells the dropdown list to select that item.

Then in the view:
@Html.DropDownList("DepartmentID", String.Empty)

I hope that helps
 
Share this answer
 
Comments
Timothy Vandeweerd 26-May-17 14:32pm    
Can someone please help? I have a similar situation:
I have the following which populates the DropDownList for each row in the model yet does not set the selected value (tblcusts.typeid) in the DropDownList when the page is populated. Any assistance would be greatly appreciated.

Models

public class tblcusts
{
[Key]
public int custid { get; set; }
public int typeid { get; set; }
}

public class tblcusttypes
{
[Key]
public int typeid { get; set; }
public string name { get; set; }
public bool active { get; set; }
}

View

@model IList<mvcapplication1.models.tblcusts>
@for (var i = 0; i < Model.Count; ++i)
{
@Html.DropDownList("typeid", (IEnumerable<selectlistitem>)ViewBag.customertype)

}

Controller

var result = from m1 in db.custs select m1;

var query = from m2 in db.custtypes where m2.active == true orderby m2.name select m2.name;
var CustomerTypeList = new List<string>();
CustomerTypeList.AddRange(query.Distinct());
ViewBag.customertype = new SelectList(CustomerTypeList);

return View(result.ToList());

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900