2012-01-11 81 views
0

我有简单的表单。它包含几个texboxes和一个下拉列表。而我不能得到选定的项目ID将其保存到数据库中。无法获取所选物品ID

这是控制器方法whitch displaing形式

public ActionResult DisplayForm() 
     { 
      ViewBag.State = new SelectList(conn.state, "STATE_Id", "STATE_Name"); 
      return View(new order_data()); 
     } 

这是方法whitch应的数据保存到数据库

[HttpPost] 
     public ActionResult MakeOrder(order_data data, state stateId) 
     { 
      if (ModelState.IsValid) 
      { 
       conn.order_data.Add(data); 
       conn.SaveChanges(); 
       RedirectToAction("Index", "Home"); 
      } 
      return View(); 
     } 

这是我的视图

@model bookstore.order_data 

@{ 
    ViewBag.Title = "Dane osobowe"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<h2>DisplayForm</h2> 

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 

@using (Html.BeginForm("MakeOrder","Order")) 
{ 
    @Html.ValidationSummary(true) 
    <fieldset> 

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

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

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

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


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


     <div class="editor-label"> 
      @Html.LabelFor(model => model.state) 
     </div> 
     @Html.DropDownList("State", "--wybierz województwo--") 



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

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

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

     <div class="editor-label"> 
      @Html.LabelFor(model => model.ORDA_ZIP) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.ORDA_ZIP) 
      @Html.ValidationMessageFor(model => model.ORDA_ZIP) 
     </div> 
     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 
} 

,这是模型whitch包含客户数据

namespace bookstore 
{ 
    public partial class order_data 
    { 
     public order_data() 
     { 
      this.orders = new HashSet<orders>(); 
     } 
     [Key] 
     public int ORDA_ID { get; set; } 
     [ForeignKey("state")] 
     public Nullable<int> State_STATE_Id { get; set; } 
     [Required] 
     [DisplayName("Miasto")] 

     public string ORDA_CityName { get; set; } 
     [Required] 
     [DisplayName("Ulica")] 
     public string ORDA_StreetName { get; set; } 
     [Required] 
     [DisplayName("Numer ulicy")] 
     public string ORDA_StreetNr { get; set; } 
     [Required] 
     [DisplayName("Numer domu")] 
     public string ORDA_HomeNr { get; set; } 
     [Required] 
     [DisplayName("Kod pocztowy")] 
     public string ORDA_ZIP { get; set; } 
     [Required] 
     [DisplayName("Email")] 
     public string ORDA_Email { get; set; } 
     [Required] 
     [DisplayName("Imię")] 
     public string ORDA_Name { get; set; } 
     [Required] 
     [DisplayName("Nazwisko")] 
     public string ORDA_LastName { get; set; } 
     [Required] 
     [DisplayName("Telefon")] 
     public string ORDA_Phone { get; set; } 
     [Required] 
     [DisplayName("Województwo")] 
     public virtual state state { get; set; } 
     public virtual ICollection<orders> orders { get; set; } 
    } 

} 

,这是模型包含下拉列表数据

public partial class state 
    { 
     public state() 
     { 
      this.order_data = new HashSet<order_data>(); 
     } 

     public int STATE_Id { get; set; } 
     public string STATE_Name { get; set; } 

     public virtual ICollection<order_data> order_data { get; set; } 
    } 

顺便说一句,我需要传递的OrderID到控制器太多,我不知道怎么了,becouse ID总是等于0

请,帮助

回答

1

是不是OrderId(ORDA_ID)是您的order_data表中的一个KEY?所以当你在表格中插入一行时就会分配它。所以要么我不完全理解你的问题,要么你没有试图将orderId传递给控制器​​。

你执行

conn.order_data.Add(data); 
conn.SaveChanges(); 

后,你将能够得到您ORDA_ID:

int orderId = data.ORDA_ID; 

,如果这是你所需要的

+0

是,订单ID是表order_data的主键。问题是我不能将数据添加到表中,因为主键不设置 – lukaszb 2012-01-11 23:11:40

+0

您使用DB-first或Code First方法吗?如果错误提示没有设置ORDA_ID,并且您首先使用了数据库(部分类的使用会显示这种情况),那么我会说您错过了您的PK – torm 2012-01-11 23:23:14

+0

上的Identity选项,我没有错误。当我创建数据库时,我在此列上设置了身份选项。当我debbug它ORD_Id = 0,总是......我的ModelState无效。但我认为与国家领域有关。 – lukaszb 2012-01-11 23:26:57