2016-08-02 35 views
1

我使用mvc 5作爲學習目的。 當我從下拉列表中發送控制器上的數據時,對象在控制器中具有空值。mvc5中的html幫手下拉控件的空值後

型號代碼:

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

     public partial class Country 
     { 
      public int ID { get; set; } 
      public string CountryName { get; set; } 
     } 
    } 

控制器代碼:

namespace Dropdownlist.Controllers 
    { 
     public class HOMEController : Controller 
     { 
      DropDownEntities db = new DropDownEntities(); 
      public ActionResult Index() 
      { 
       return View(); 
      } 
      [HttpPost] 
      public ActionResult Index(Country cn) 
      { 
       db.Countries.Add(cn); 
       db.SaveChanges(); 
       return View(cn); 
      } 
     } 
    } 

查看代碼:

@{ 
     ViewBag.Title = "Index"; 
    } 

    <h2>Index</h2> 
    @using (Html.BeginForm(FormMethod.Post)) 
    { 
     <div> 
      @Html.DropDownList("ddlcountry", new List<SelectListItem> 
      { 
       new SelectListItem{ Text = "India", Value = "India"}, 
       new SelectListItem{ Text = "UK", Value = "UK"}, 
       new SelectListItem{ Text = "USA", Value = "USA"} 
      }, "Select a Country") 
     </div> 
     <div> 
      <input type="submit" value="Save" /> 
     </div> 
    } 

我在做什麼錯?

+0

你需要ID進行設置? – Hamed

+0

爲什麼你認爲它應該把數據發送給控制器 –

回答

0

如果你想設置的CountryName屬性更改您的視圖這樣的:

.... 
    @Html.DropDownListFor(x=>x.CountryName, new List<SelectListItem> 
.... 
+0

這不會工作,因爲id不會被設置。 –

+0

你不能同時設置兩個屬性,我看到'Value'是一個字符串類型,所以我想它會是'CountryName'。 – Hamed

+0

該行爲應該接受一個字符串,而不是一個國家對象。 –

0

您在這裏有幾個問題。該下拉列表名稱爲「ddlcountry」,但操作期望的是沒有ddlcountry屬性的Country對象。 ddlcountry是一個字符串印度/英國/美國。表單應該返回交叉引用另一個表的Id。由於只有一條數據,因此它的形式沒有任何意義。

0

負載從控制器下拉數據

ViewBag.DropDown = db.YourModel.ToList(); 

,然後在您的視圖

@Html.DropDownList("Name", (IEnumerable<SelectListItem>)ViewBag.DropDown, "Select ...") 
1

如果要發佈兩種IDCountryName值回,那麼你就需要有控件名稱與您的模型的屬性相匹配,並且您的視圖應該是強類型的,以便您可以使用Html.DropDownListFor()幫手,現在您可以這樣做:

@model Dropdownlist.Models.Country 
@{ 
     ViewBag.Title = "Index"; 
    } 

    <h2>Index</h2> 
    @using (Html.BeginForm(FormMethod.Post)) 
    { 
     <div> 
      @Html.DropDownList("ID", new List<SelectListItem> 
     { 
      new SelectListItem{ Text = "India", Value = 1}, 
      new SelectListItem{ Text = "UK", Value = 2}, 
      new SelectListItem{ Text = "USA", Value = 2} 
     }, "Select a Country",new { id="ddlCountry"}) 

     @Html.Hidden("CountryName") 
     </div> 
     <div> 
      <input type="submit" value="Save" /> 
     </div> 
    } 

和國家名稱,你需要將其設置在一個隱藏字段,並設置它的值時,下拉指數變了樣:

@section Scripts 
{ 

<script type="text/javascript"> 

    $(document).ready(function() { 

     $("#ddlCountry").on("change", function() { 

      $("#CountryName").val($(this).val()); 

     }); 

    }); 
    </script> 

} 
0

這裏還有幾個問題,但我認爲你這樣的事情後:

您選擇列表項的枚舉:

public enum Countries 
    { 
     India = 1, 
     UK = 2, 
     USA = 3 
    } 

然後控制器動作:

public ActionResult Index() 
    { 
     ViewBag.Country = Enum.GetValues(typeof(Countries)).Cast<Countries>().ToList().Select(r => new SelectListItem { Text = r.ToString(), Value = ((int)r).ToString() }); 

     return View(); 
    } 

    [HttpPost] 
    public ActionResult Index(Countries country) 
    { 
     var saveit = country; 
     // whatever you wish to do with the result; 
     return Content(saveit.ToString()); 
    } 

的觀點:

@using(Html.BeginForm("Index", "Home", FormMethod.Post)) 
{ 
    @Html.DropDownList("Country") 
    <button type="submit" >Save</button> 
}