0

I am viewing my records on the index with a PagedList

// GET: Product
    public ActionResult Index(int page = 1)
    {
        int recordsPerPage = 1; //Page size = 1 record
        var productList = db.q_product.ToList().ToPagedList(page, recordsPerPage);             
        return View(productList);
    }

when viewing the records on the page the fields are editable as seen in the image below

product view on page

The functionality I am trying to create is to be able to edit any field and click save which will in turn update that record in the database and return me back to that same record after post/click save event.

    <div class="form-group" style="float: left;">                
                <input type="submit" value="Save" class="btn btn-default" />                    
    </div>

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Save(q_product q_product, int page = 1)
    {
        if (!ModelState.IsValid)
        {
            int recordsPerPage = 1;
            var productList = db.q_product.ToList().ToPagedList(page, recordsPerPage);
            return View(productList);
        }
        else
        {
            db.Entry(q_product).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }            
    }

How do I got about achieving this functionality?

EDIT:

The records are in a form as seen with the following code

 <h4>PRODUCT VIEW</h4>

 @foreach (var item in Model)
 {
     using (Html.BeginForm("Save", "Product"))
      {
         @Html.AntiForgeryToken()
         @Html.ValidationSummary(true, "", new { @class = "text-danger" })
         @Html.HiddenFor(model => model.FirstOrDefault().q_guid)

        <div class="panel panel-default">
            <div class="panel-heading">PRODUCT INFORMATION</div>
            <div class="row">
                <div class="col-lg-4">
                    <div class="panel-body">
                        <div class="form-group">
                            <dt>
                                @Html.DisplayNameFor(model => model.FirstOrDefault().q_barcode)
                            </dt>
                            <dd>
                                @Html.EditorFor(modelItem => item.q_barcode)
                            </dd>
                        </div>
                    </div>
                </div>
                <div class="col-lg-4">
                    <div class="panel-body">
                        <div class="form-group">
                            <dt>
                                @Html.DisplayNameFor(model => model.FirstOrDefault().q_description)
                            </dt>
                            <dd>
                                @Html.EditorFor(modelItem => item.q_description)
                            </dd>
                        </div>
                    </div>
                </div>
LavsTo
  • 129
  • 4
  • 19
  • You need a form and submit button to post to your `Save()` method (not a link which makes GET) –  Apr 19 '17 at 21:24
  • Ah yes, I forgot to mention my records are in a form (updated question with the code). I copied the wrong button code/line. My bad. – LavsTo Apr 19 '17 at 21:30
  • That code in the form will never work - its creating form controls which have no relationship to your model. What is the point of this. Just have an index view that displays all records and an associated `'Edit' link that redirects you to an Edit view –  Apr 19 '17 at 21:32
  • Its just requested how the page is required to function (for learning purposes). I have no problem using going through an edit view and saving the file. That works perfectly fine. I am curious to know if this way is possible. – LavsTo Apr 19 '17 at 21:36
  • Not without a lot of hacks –  Apr 19 '17 at 21:39
  • now I am curious to know these hacks :). Time for google – LavsTo Apr 19 '17 at 21:49
  • @StephenMuecke I was able to solve this with answer from you on this post https://stackoverflow.com/a/25275968/7610106 – LavsTo May 23 '17 at 09:36

0 Answers0