2016-08-02 34 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> 
}