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

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Google Amazon and Flipkart Book Store!

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.
ASK Questions
SQL Server NodeJs TypeScript JavaScript Angular SQL Server My Book