how to get values from formcollection in asp.net mvc 4

How to get values from formcollection in asp.net mvc 4?

Hello everyone, I am going share the code sample to get values from formcollection in mvc 4 and all the detail as given below.

Update Devices Controller code for update Devices using FormCollection.

// 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>



ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

www.code-sample.com/. Powered by Blogger.
^