3

I have controller Action Method like below wil return all details from DB.

 public ActionResult Index()
    {
        BusDataContext db = new BusDataContext();
        List<BusDetails> buses = db.Bus.ToList();
        return View(buses);
    }

Which will return list of details to Main view which is Strongly typed view like below.

@model IEnumerable<MVC_DAL.BusDetails>
   <p>
     @Html.ActionLink("Create New", "AddBus")
   </p>
 <table class="table">
 <tr>
    <th>
        @Html.DisplayNameFor(model => model.BusSrlNo)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.BusName)
    </th>
    <th></th>
</tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelIte => item.BusSrlNo)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.BusName)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.BusSrlNo }) |
        @*@Html.ActionLink("Details", "Details", new { id = item.BusSrlNo }) |*@
        @Ajax.ActionLink("Details", "Details", new { id = item.BusSrlNo }, new AjaxOptions{UpdateTargetId="update" })
        @Html.ActionLink("Delete", "Delete", new { id = item.BusSrlNo })

      </td>
  </tr>
 }

</table>

    <div id="update">

    </div>

With Model Class as

public class BusDetails
{
    public int BusSrlNo { get; set; }
    public string BusName { get; set; }
}

And Details ActionMethod consists

public PartialViewResult Details(int id)
    {
        BusDataContext detail = new BusDataContext();       
        BusDetails bsdtledit = detail.Get_Edit(id);
        return PartialView("Details",bsdtledit);

    }

Corresponding partial view with above model class is below:

   @model MVC_DAL.BusDetails

 <div>
   <h4>BusDetails</h4>
   <hr />
   <dl class="dl-horizontal">
    <dt>
        @Html.DisplayNameFor(model => model.BusSrlNo)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.BusSrlNo)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.BusName)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.BusName)
    </dd>

  </dl>
 </div>
 <p>
    @Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
    @Html.ActionLink("Back to List", "Index")
 </p>

So overall i need to display above partial view in Main view only after i click Details link action which is there in Main view.Above code will Display details in seperatetly without main view rather than updating in same page at div (id="update")section. So how do i update in same Main View..?

Abhi
  • 785
  • 1
  • 10
  • 19

3 Answers3

3

That's where Microsoft came out with the Ajax.ActionLink. That's one option; I personally like jQuery better, so I do something like:

$.ajax({

   type: "get",
   url: "@Url.Action("Partial", "Controller"),
   success: function(d) { 
     /* d is the HTML of the returned response */
     $("#SomeParentIDForSection").html(d); //replaces previous HTML with action
   }
});

Just make sure your controller returns a partial view:

public ActionResult Partial()
{
    var model = ..
    //init work

    return PartialView(model);
}

See these references for more information and some other approaches:

Community
  • 1
  • 1
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • Still I am not getting..My ajax is like below. $(function () { $('.js-bus-details-link').click(function () { $.ajax({ type: "GET" url:"@Url.Action("Details", "Home") success:function(e){ $("#update").html(e); } }); });But still opening in new page only.Here #update is target div in main view only.. – Abhi Jun 11 '16 at 18:26
  • Is js-bus-details-link a hyperlink? You have to prevent the default behavior by changing the handler to `click(function(arg) {` and then calling `e.preventDefault();` – Brian Mains Jun 12 '16 at 11:25
  • You don't want the hyperlink to redirect; so preventDefault should do it or use # instead in the HREF. – Brian Mains Jun 12 '16 at 11:26
  • Thanks for reply Brian.I changed as u told ,but No use.I changed like $('.js-bus-details-link').click(function (args) { $.ajax({ type: 'get', url: '@Url.Action("Details", "Home")', success: function (e) { e.preventDefault(); $("#update").html(e); } }); }); where 'js-bus-details-link' is just class in line @Ajax.ActionLink("Details","Details", new { id = item.BusSrlNo, @class = "js-bus-details-link" }....) .So plz help me. – Abhi Jun 12 '16 at 12:00
  • Even I put breakpoint above inside in script tag,which is not hitting during execution...it's giving error like breakpoint currently will not be hit.No executable code of the debugger target code type is associated with this line. – Abhi Jun 12 '16 at 12:02
  • Then it's possible the selector isn't finding an element with class js-bus-details-link, or there are times where breakpoints in JS fails in Visual Studio. – Brian Mains Jun 12 '16 at 12:34
  • Also I checked in Controller Is ajax request is passing by writing if (Request.IsAjaxRequest()) { return PartialView(bsdtl); }where bsdtl is object of class. But control is not entering inside if loop.So Its treating as a non-ajax request.So what should I do..? – Abhi Jun 12 '16 at 12:36
  • Throw an alert("HERE"); in just to be sure it isn't hitting it, then look at the view source to make sure that class is absolutely defined the correct way.... I've done what you are trying but occasionally you'll encounter an issue, and sometimes that issue is on the server-side. – Brian Mains Jun 13 '16 at 02:27
  • Finally its working..I added .Thanks Brian – Abhi Jun 26 '16 at 09:51
1

Supposing that you will show you partial view in the following div:

<div class="js-bus-details">
</div>

You could make an ajax call and update the html inside this div. This can be done like below:

@Html.ActionLink("Details", "Details", new { id = item.BusSrlNo, @class="js-bus-details-link" })

and then place the following script at the bottom of your page.

<script>
$(function(){
    $(".js-bus-datails-link").click(function(){
        var busId = $(this).data("id");
        $.ajax({
            method: "GET"
            url: "", // Here you have to place the relative url to your action
            data: { id: busId }
            cache: false
            success: function(e){
                $(".js-bus-details").html(e);
            }
        });
    })
})
</script>
Christos
  • 53,228
  • 8
  • 76
  • 108
  • Still I am not able to do it. It's opening in new page only..not updating in same page... – Abhi Jun 11 '16 at 18:07
  • And the URL on which it's redirecting is ' http://localhost:55138/Home/Details/3?class=js-bus-details-link ' where '3' is Id of the text which I clicked. – Abhi Jun 11 '16 at 18:14
  • Even breakpoint I put in above inside script tag,which is not hitting during execution...its giving error like breakpoint currently will not be hit.No executable code of the debugger target code type is associated with this line..plz reply – Abhi Jun 12 '16 at 10:01
1

by click event in javascript:

$("#myCodes").load('@Url.Action("PartialViewResult", "Controller_Name")');

N.B.: PartialViewResult must return PartialView(model); and .cshtml page a partial page

currarpickt
  • 2,290
  • 4
  • 24
  • 39
AbdusSalam
  • 420
  • 6
  • 10