I have simple form. It contains few texboxes and one dropdown list. And I cant get selected item id to save it into database.
This is controller method whitch displaing a form
public ActionResult DisplayForm()
{
ViewBag.State = new SelectList(conn.state, "STATE_Id", "STATE_Name");
return View(new order_data());
}
this is method whitch should save data into database
[HttpPost]
public ActionResult MakeOrder(order_data data, state stateId)
{
if (ModelState.IsValid)
{
conn.order_data.Add(data);
conn.SaveChanges();
RedirectToAction("Index", "Home");
}
return View();
}
This is my 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>
}
and this is model whitch contains client data
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; }
}
}
and this is model which contains dropdown list data
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; }
}
By the way, i need to pass a orderid into controller too and i dont know how, becouse id always equals 0
Please, help
Isn’t orderId (ORDA_ID) a KEY in your order_data table ? So it’s being assigned when you insert a row into the table. So either I don’t fully understand your question or you’re not trying to pas orderId into the controller.
after you execute
you will be able to get your ORDA_ID by :
if this is what you need