-1

I need to send send value of particular row to controller method on clicking of anchor buttons. Also I need functionality to filter data based on dropdown selection(Working fine). I am newbie to asp mvc, Do not know if I am doing right if there is any BETTER solution without Jquery tables please suggest.

Here is my view structure:

    @using (Html.BeginForm("Index", "Manage_Menu", FormMethod.Post, new { id = "myForm" }))
{<div style="float:left;padding-bottom:10px;">
        <b>Select Parent Page</b>
        <div>

            @Html.DropDownList("ddlPageId", (IEnumerable<SelectListItem>)ViewBag.PageDDL, "Select parent page", new { onchange = "this.form.submit();" })

        </div>
    </div>
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.PageName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.IsActive)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ShowInMenu)
            </th>
            <th>Move Menu Position</th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.PageName)
                </td>
                <td>
                    <a href="javascript:void()" onclick="sumitForm()">
                        <input type="hidden" name="mmmm" value="@item.Id" />
                        @if (item.ShowInMenu == true)
                        {
                            <span class="glyphicon glyphicon-ok text-success" aria-hidden="true"></span>
                        }
                        else
                        {
                            <span class="glyphicon glyphicon-remove text-danger" aria-hidden="true"></span>
                        }
                    </a>
                </td>
                <td>
                    <a href="javascript:void()" onclick="sumitForm()">
                        @if (item.ShowInMenu == true)
                        {
                            <span class="glyphicon glyphicon-ok text-success" aria-hidden="true"></span>
                        }
                        else
                        {
                            <span class="glyphicon glyphicon-remove text-danger" aria-hidden="true"></span>
                        }
                    </a>
                </td>
                <td>
                    <a href="javascript:void()" onclick="sumitForm()">
                        <span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span>
                    </a>
                    <a href="javascript:void()" onclick="sumitForm()">
                        <span class="glyphicon glyphicon-arrow-down" aria-hidden="true"></span>
                    </a>
                </td>
            </tr>
        }

    </table>

    <script>
        function sumitForm() {
            document.getElementById("myForm").submit();
        }
    </script>
}

Here is my Controller:

public ActionResult Index()
        {
            var pages = db.PageMains.Where(a => a.ParentPageId == 0); ;
            ViewBag.PageDDL = new SelectList(db.PageMains.Where(r => r.ParentPageId == 0), "Id", "PageName");
            return View(pages);
        }

        [HttpPost]
        public ActionResult Index(FormCollection aa)
        {
            if (!string.IsNullOrEmpty(aa["ddlPageId"]))
            {

                int filter = Convert.ToInt32(aa["ddlPageId"]);
                var pages = db.PageMains.Where(a => a.ParentPageId == filter);
                ViewBag.PageDDL = new SelectList(db.PageMains.Where(r => r.ParentPageId == 0), "Id", "PageName", filter);
                return View(pages);
            }
            else
            {
                var pages = db.PageMains.Where(a => a.ParentPageId == 0); ;
                ViewBag.PageDDL = new SelectList(db.PageMains.Where(r => r.ParentPageId == 0), "Id", "PageName");
                return View(pages);

            }


        }

I have tried to store values inside hidden field but whenever I press any anchor button it is sending all the values.

Here is my page design: enter image description here

Tony smith
  • 49
  • 3
  • 13
  • Pass the ID to where? –  Jul 23 '16 at 05:52
  • You view is not making sense and since the form is being submitted when you select an option from the dropdownlist. Sorry to be harsh but almost everything you doing is bad practice. What is the purpose of sending the `ID` to the method? –  Jul 23 '16 at 06:45
  • I need to change status of IsActive and ShowInMenu in database. – Tony smith Jul 23 '16 at 06:51
  • Yeah It might be bad practice... totally new to mvc.. guide please – Tony smith Jul 23 '16 at 06:51
  • Which is a completely different action that filtering your records (which should be GET not a POST anyway). And your should be using checkboxes and allow the user to check or uncheck any of the `IsActive` or `ShowInMenu` items in any or all of the rows and save all the changes at once. –  Jul 23 '16 at 06:53
  • I suggest you study the code in [this DotNetFiddle](https://dotnetfiddle.net/mOvx1n) to get you started. Note you can re-order the table rows by drag and drop. I did not implement the `.change()` event for the dropdownlist, but you would use a jquery method and use ajax to call a server method that updates the `` element based on the selected option value. –  Jul 25 '16 at 07:05
  • @StephenMuecke your DotNetFiddle is not working. Giving me "Fatal Error: Execution time limit was exceeded" error. Thanks for introducing me with dotnetfiddle. – Deepak Kumar Jul 27 '16 at 15:04
  • @DeepakKumar, the site gets overloaded occasionally. It working fine right now. –  Jul 29 '16 at 07:23

2 Answers2

0

The problem here is that you have a single form and all the anchors have the code to submit the form on click. So your click on any of the anchors lead to entire form submission - hence submitting all the data.

If you want partial data submission you can very well use ajax post. Pass the value using the ajax post and based on the return from controller update the table.

Since you are new to the technology I would suggest taking some time to read about ajax posts: https://api.jquery.com/jquery.post/

Also for more dynamic client side bindings you can use client side frameworks like AngularJS

OR

A very bad way would be to have forms for all the rows and name them based on the element's Id and on click submit only the form specific to the row. But this would be pathetic code.

0

What @Stephen Muecke is suggesting is the ideal situation to update the content in one action.

However, sometimes we need to update content in real time. So to handle your case jQuery is the best bet. Since you asked to do it using MVC, below is the proposed solution (Ironically, under the hood jQuery is doing the work for you)

You need to use @Ajax.ActionLink method provided in MVC to make ajax calls. To make it a real Ajax, you need to add reference to jquery.unobtrusive-ajax.min.js. Otherwise it will fallback to Postback request

E.g.

Script Reference needs to be added.

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

Your action button in view would look like:

@Ajax.ActionLink("Click Me", "Clicked", "Home", new { id = item.pageId})

Action Method

        public void Clicked(string id)
        {
        //Save the page ID in database. I keep it simple but if you want, you can return the status too. 
        }

One drawback of using @Ajax.ActionLink is, you cannot set html inside anchor tag like you did in your example (e.g. span tag). If you want to achieve that too refer here

Community
  • 1
  • 1
Deepak Kumar
  • 615
  • 1
  • 5
  • 20
  • As you have guided. I have converted my Code and added links like this: @Html.ActionLink("Edit", "Edit", new { id = item.Id }) ---- Now problem is dropdown is losing its selected value... How to get dropdown value in edit method? – Tony smith Jul 27 '16 at 13:05
  • I think I had suggested Ajax.ActionLink but it seems you have used Html.ActionLink. Html.Action method cause full page postback thus drop down loses it's selected value. Ajax.Action method only make an Ajax call without full page submission. – Deepak Kumar Jul 27 '16 at 15:02
  • Hi Deepak, I have converted code to Ajax.ActionLink. Everything working fine. Except this one: How to refresh table after button click? – Tony smith Jul 28 '16 at 07:35
  • I have tried " return RedirectToAction("Index");" in the end of method. But in this case I lost selected value of dropdownlist – Tony smith Jul 28 '16 at 07:36
  • @Tonysmith I am sorry but if I see your question, it asked for exploring ways to send data for just one row instead of sending full page information. Now you are asking to refresh the page after ID update. Maybe your current needs have been evolved since raising this question here in stack. You should revisit your question and update it with evolved requirement. Then only would be able to help you further. – Deepak Kumar Jul 28 '16 at 10:29
  • Thanks Deepak for your help. I was just curious to reload my table inside public void Clicked(string id) {} – Tony smith Jul 28 '16 at 13:06
  • Thanks @Tonysmith. There are many ways to achieve this. If I were in your shoes then I would use jQuery for this. Write callback functions to refresh the table and call those functions using jQuery once we update the ID in DB. – Deepak Kumar Jul 28 '16 at 15:25