0

I'm trying to make a page using ASP MVC 5 with C# in which I have some controls to type search parameters and a table where the results are shown.

The thing is that the user should be allowed to edit the text of two fields in the table and save the changes.

To accomplish this, my view receives a model with two objects as properties, one for the search parameters and one for the result list, like this:

public class SEARCH_PAGE
{
    public List<VIEW_DATA_APR> table{ get; set; }
    public SEARCH parameters{ get; set; }
}

Then my Controller is this:

public class CargaAPRController : Controller
{
    private datasource db = new datasource();
    // GET: CargaAPR
    public ActionResult Index()
    {
        try
        {
            List<SelectListItem> items = new SelectList(db.COMPANY, "IDCOMPANY", "COMPANYNAME").ToList();
            items.Insert(0, (new SelectListItem { Text = "ALL", Value = "0" }));
            ViewData["IDCOMPANY"] = items;
            var letters = (from c in db.LETTERS
                             select new VIEW_DATA_APR
                             {
                                 company = c.COMPANY.COMPANYNAME,
                                 idLetter = c.IDLETTER,
                                 nic = "not found",
                                 client = "not found",
                                 energy = 0,
                                 money = 0,
                                 period = "",
                                 letterCode = c.LETTERCODE
                             });

            SEARCH_PAGE sp = new SEARCH_PAGE();

            sp.table= letters.ToList();
            sp.parameters= new SEARCH();

            return View(sp);
        }
        catch (Exception ex)
        {
            return RedirectToAction("Error", new RouteValueDictionary(new { controller = "Users", action = "Error", mensaje = "Error: " + ex.Message }));
        }

    }

    [HttpPost]
    public ActionResult Index(SEARCH_PAGE model)
    {
        try
        {
            List<SelectListItem> items = new SelectList(db.COMPANY, "IDCOMPANY", "COMPANYNAME").ToList();
            items.Insert(0, (new SelectListItem { Text = "ALL", Value = "0" }));
            ViewData["IDCOMPANY"] = items;

            decimal company = Decimal.Parse(model.parameters.company);
            var letters= (from c in db.LETTERS
                             where (company== 0 ? c.IDCOMPANY: company) == c.IDCOMPANY
                             select new VIEW_DATA_APR
                             {
                                 company= c.COMPANY.COMPANYNAME,
                                 idLetter= c.IDLETTER,
                                 nic = "not found",
                                 client = "not found",
                                 energy = 0,
                                 money = 0,
                                 period = "",
                                 letterCode = c.LETTERCODE
                             });

            SEARCH_PAGE sp = new SEARCH_PAGE();

            sp.table= letters.ToList();
            sp.parameters = model.parameters;

            return View(sp);
        }
        catch (Exception ex)
        {
            return RedirectToAction("Error", new RouteValueDictionary(new { controller = "Users", action = "Error", mensaje = "Error: " + ex.Message }));
        }

    }

    [HttpPost]
    public ActionResult Save(SEARCH_PAGE model_search_page )
    {
        return View();
    }
}

And my View is:

@using datasource.Models
@model SEARCH_PAGE
@{
ViewBag.Title = "Load APR";
Layout = "~/Views/Shared/_Layout.cshtml";
}

@Styles.Render("~/Content/energetica.css")
<meta name="viewport" content="width=device-width" />
<title>Letters</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<div class="container-fluid">
    <div class="col-md-1">
</div>
<div class="col-md-10">
    <div class="panel panel-default">
        <!-- Default panel contents -->
        <div class="panel-heading">
            <div class="row">
                <div class="col-md-1">
                    <a href="@Url.Action("Index", "LoadFiles")" class="elements">
                        <img class="img img-responsive" style="width:25px; height:26px;padding:1px" src="~/img/BackIcon.png">
                    </a>
                </div>
                <div class="col-md-11 text-left" style="padding:1px;">
                    LOAD APR
                </div>
            </div>
        </div>
    </div>
    @using (Html.BeginForm("Index","LoadAPR", FormMethod.Post, null))
    {
        <table style="width:100%">
            <tr>
                <td style="width:10%">
                    <b>COMPANY: </b>
                </td>
                <td style="width:20%">
                    @Html.DropDownListFor(m => m.parameters.company, (IEnumerable<SelectListItem>)ViewData["IDCOMPANY"], new { htmlAttributes = new { @class = "form-control" } })
                </td>
                <td style="width:10%">
                    <b>PERIOD: </b>
                </td>
                <td style="width:20%">
                    @Html.EditorFor(m => m.parameters.period)
                </td>
                <td style="width:20%; text-align:right">
                    <input  type="submit" name="SEARCH" value="SEARCH" />
                </td>
            </tr>
            <tr>
                <td style="width:10%">
                    <b>CLIENT: </b>
                </td>
                <td style="width:20%">
                    @Html.EditorFor(m => m.parameters.client)
                </td>
                <td style="width:10%">
                    <b>NIC: </b>
                </td>
                <td style="width:20%">
                    @Html.EditorFor(m => m.parameters.nic)
                </td>
            </tr>
        </table>
    }
    <br />
    @using (Html.BeginForm("Save", "LoadAPR", FormMethod.Post, null))
    {
        <div style="overflow-y: scroll; max-height: 300px">
            <table style="width:100%">
                <tr>
                    <th>
                        @Html.Label("LBDIS", "Company")
                    </th>
                    <th>
                        @Html.Label("LBNLETTER", "Letter Code")
                    </th>
                    <th>
                        @Html.Label("LBNIC", "NIC")
                    </th>
                    <th>
                        @Html.Label("LBCLIENT", "Client")
                    </th>
                    <th>
                        @Html.Label("LBENERGY", "Energy")
                    </th>
                    <th>
                        @Html.Label("LBMONEY", "Money")
                    </th>
                </tr>
                @foreach (var item in Model.table)
                {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.company)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.letterCode)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.nic)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.client)
                        </td>
                        <td>
                            @Html.EditorFor(modelItem => item.energy)
                        </td>
                        <td>
                            @Html.EditorFor(modelItem => item.money)
                        </td>
                    </tr>
                }
            </table>
        </div>
        <br />
        <div style="width:100%; text-align:right;">
            <input class="btn-addUser" type="submit" value="Save" />
        </div>
    }
</div>

So when I run it and click on my first button, the POST Index parameter receives just the parameters part of the object, the table part is null

And when I click on my second button, the POST Save parameter comes null in both properties.

I have tried so many different ways of seding the Model as parameter but nothing is working. Could you tell me what's wrong? Am I using he BeginForm in the correct way?

Thank you in advance.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Francisco
  • 1
  • 2
  • First there is no issues with either of your posts. Second, only `parameters` are passed to Index, this is correct. The `table` in the `model` is not rendered in the `form`, so doesn't post this. Third, the form `LoadAPR` contains no `inputs` all of them are just displaying data. So nothing will come through to your post. – Searching Nov 15 '16 at 00:09
  • Have you tried to add a @Html.HiddenFor() for the values you want to post? – BasicIsaac Nov 15 '16 at 00:19
  • Check out this http://stackoverflow.com/q/30401500/1339516 for updating multiple records – Searching Nov 15 '16 at 00:37
  • You have 2 separate forms. A form only posts back the values of its form controls within its `
    ` element - you will never get the values of the 2nd form in the first method and vice versa. And the reason why the 2nd method parameters are not bound is because you cannot use a `foreach` loop to generate form controls for a collection (refer [Post an HTML Table to ADO.NET DataTable](http://stackoverflow.com/questions/30094047/post-an-html-table-to-ado-net-datatable/30094943#30094943))
    –  Nov 15 '16 at 02:02
  • Thank you everybody, replacing the foreach loop by a for loop solved my problem – Francisco Nov 16 '16 at 17:55

0 Answers0