0

I'm new to asp.net. I´ve made a Home View which displays a list of items. In the same View I have a partial view.

@{
     ViewBag.Title = "Home Page";
}
<div class="row">
    <div class="col-md-4">
        <h2>Geräte Liste</h2>
            @model IEnumerable<MDAVerwaltung.Models.Geraete>
            @{
                 ViewBag.Title = "Index";
            }
            <!--url: "{controller}/{action}/{id}", -->
            <table class="table">
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.DeviceID)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Bezeichnung)
                </th>
                <th></th>
            </tr>

            @foreach (var item in Model)
            {
                <tr>
                    <td>

                        @Html.DisplayFor(modelItem => item.DeviceID)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Bezeichnung)
                    </td>
                </tr>
            }
        </table>
   </div>
   <div class="col-md-8">
    <div class="col-md-8">
        <h2>Geräte Informatioen</h2>

        @Html.Action("GetDeviceDetails")

    </div>

    <div class="col-md-4">
        <h2>Zusatzinformationen</h2>
    </div>
</div>
</div>

You see the partial view is called with @Html.Action("GetDeviceDetails"). Now i got on the left my List of Devices and i want to click on one Item and update the detail view.

The home Controller looks like this:

using System.Data.Entity;
using System;
using System.Net;

namespace MDAVerwaltung.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var geraete = db.Geraete.Include(g => g.Mitarbeiter);
            return View(geraete.ToList());
        }
        private MDAEntities db = new MDAEntities();
        // GET: DeviceList
        public ActionResult GetDeviceList()
        {
            var geraete = db.Geraete.Include(g => g.Mitarbeiter);
            return View("_DeviceList",geraete.ToList());
        }
        public ActionResult GetDeviceDetails(int? id = 4)
        {
            Geraete geraete = db.Geraete.Find(id);
            return View("_DeviceDetails",geraete);
        }
        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";
            return View();
        }
    }
}

Like you see the Home view and the partial view uses the same controller. The partial view looks like this:

Geräte Informationen123
@model MDAVerwaltung.Models.Geraete
<p>@Html.DisplayFor(model => model.Bezeichnung)</p>
<p>@Html.DisplayNameFor(model => model.DeviceID)</p>

But how can I pass data from the list to the partial view to update these? Can someone please explain these or give me a link to a tutorial?

3
  • In the ActionResult GetDeviceDetails, replace return View(...) by return PartialView(...) Commented Apr 30, 2015 at 11:25
  • Okay, I added <pre>@Html.ActionLink("as","GetDeviceDetails", new { id = item.ID}) <code> to the list Items. In the Controller i Changed <pre>return View("_DeviceDetails",geraete) <code> to <pre> return PartialView("_DeviceDetails",geraete); <code>. Now the Parameter are passed, but the Partial View is shown in a new Window. But I want to refresh it in the Home View. Commented Apr 30, 2015 at 12:24
  • @PBS did you get a chance to try out using Ajax.ActionLink? Commented Apr 30, 2015 at 22:22

1 Answer 1

1

Wrap your partial view with div with id:

<div id="deviceDetailId">
   @Html.Action("GetDeviceDetails")
</div>

Then use Ajax helper to fetch data and update:

        @foreach (var item in Model)
        {
            <tr>
                <td>

                    @Html.DisplayFor(modelItem => item.DeviceID)
                </td>
                <td>                        
                    @Ajax.ActionLink(item.Bezeichnung, "GetDeviceDetails", "Home", new { id = item.DeviceID }, new AjaxOptions { UpdateTargetId = "deviceDetailId", HttpMethod = "GET" })
                </td>
            </tr>
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.