无法获得所选项目ID

时间:2012-01-11 22:41:59

标签: asp.net-mvc-3

我有简单的表格。它包含一些texbox和一个下拉列表。我无法获得所选的项目ID以将其保存到数据库中。

这是替换表格的控制器方法

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

这是将数据保存到数据库中的方法

 [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传递给控制器​​,我也不知道怎么样,因为id总是等于0

请帮助

1 个答案:

答案 0 :(得分:1)

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

执行

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

您将能够通过以下方式获得您的ORDA_ID:

int orderId = data.ORDA_ID;

如果这是你需要的