Hi,
I tried to save dropdown in the database, it save but the data is blank
Model (ItemModel)
public class ItemModel
{
public string Requester_id { get; set; }
public int item_id { get; set; }
public string req_material { get; set; }
public Nullable<int> req_quantity { get; set; }
}
View
<div id="tabs-2">
@using (Html.BeginForm("AddMaterial", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-inline">
<label for="Material" class="mr-sm-2">Material:</label>
<select>
<option value="0">Select</option>
@foreach (var item in ViewBag.material)
{
<option value='@item.ID'>@item.Material</option>
}
</select>
<span><label for="st" class="mr-sm-2">Quantity:</label></span>
<select>
<option value="0">Select</option>
@{
for (int i = 1; i <= 100; i++)
{
<option value="@i">@i</option>
}
}
</select>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</div>
</div>
}
<br />
@webGrid.GetHtml(
htmlAttributes: new { @id = "WebGrid", @class = "table table-striped table-hover" },
columns: webGrid.Columns(
webGrid.Column("No.", format: item => item.WebGrid.Rows.IndexOf(item) + 1 + Math.Round(Convert.ToDouble(webGrid.TotalRowCount / webGrid.PageCount) / webGrid.RowsPerPage) * webGrid.RowsPerPage * webGrid.PageIndex),
webGrid.Column("req_material", "Material"),
webGrid.Column("req_quantity", "Quantity")
))
</div>
Controller
[HttpPost]
public ActionResult AddMaterial(ItemModel model, string id)
{
TempData["Selected"] = "1";
id = Convert.ToString(TempData["NewID"]);
try
{
Material_Item item = db.Material_Item.Where(x => x.item_id == model.item_id).FirstOrDefault();
item = new Material_Item();
item.item_id = model.item_id;
item.Requester_id = id;
item.req_quantity = model.req_quantity;
item.req_material = model.req_material;
db.Material_Item.Add(item);
db.SaveChanges();
}
catch (Exception)
{
}
return RedirectToAction("FormRequest", "Home");
}