Hello everyone, I am going share the code sample
to get values from formcollection in mvc 4 and all the detail as given below.
// POST: /Devices/Edit/id
[HttpPost]
public ActionResult Edit(long id, FormCollection collection)
{
try
{
Devices deviceItem = new Devices();
DeviceRepository RepoDevice = new DeviceRepository();
deviceItem.OrderID = id;
deviceItem.CustomerID = Convert.ToInt64(collection["CustomerID"]);
deviceItem.Customer = collection["Customer"];
deviceItem.User = collection["User"];
deviceItem.OrderChannel = collection["OrderChannel"];
deviceItem.IMEI = collection["IMEI"];
RepoDevice.UpdateDevice(deviceItem.OrderID,
deviceItem.CustomerID, deviceItem.Customer, deviceItem.User,
deviceItem.OrderChannel, deviceItem.IMEI);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
Model
Binding on Razor View for or Bind current selected device for edit.
@model Entities.Model.Devices
@{
ViewBag.Title = "Edit
Device";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div>
@using (Html.BeginForm("Edit", "Devices"))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true)
<div class="form-group">
<div class="col-md-4">
<input class="form-control" required name="Customer" placeholder="Customer" value="@Model.Customer">
<input class="form-control" type="hidden" name="CustomerID" placeholder="CustomerID" value="@Model.CustomerID">
</div>
</div>
<div class="form-group">
<div class="col-md-4">
<input class="form-control" required name="User" placeholder="User" value="@Model.User">
</div>
</div>
<div class="form-group">
<div class="col-md-4">
<input class="form-control" required name="OrderChannel" placeholder="Order
Channel"
value="@Model.OrderChannel">
</div>
</div>
<div class="form-group">
<div class="col-md-4">
<input class="form-control" required name="IMEI" placeholder="IMEI" value="@Model.IMEI">
</div>
</div>
<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>
}
</div>