0

Unlike other questions on MVC and tabs, I'd like to combine a tab generated from a MVC View with a regular tab containing only a html div which is for a Google map.

I placed my tabs in the _Layout.cshtml file:

<div id="tabs">
<ul>
    <li>@Html.ActionLink("Businesses", "Index", "Business")</li> 
    <li><a href="#tab1" id="MapTab">Map</a> </li>
</ul>
<div class="container" id="tab2" style="margin-left: 25px;">
    <div style="margin-left: 25px;">
        @RenderBody()
        @RenderSection("scripts", required: false)
    </div>
</div>
<div class="container" id="tab1" style="margin-left: 25px;">
<table id="tablegrid1">
<tr>
    <td>
        <div id="map" class="mapclass" style="width: 850px;height: 500px;">                                               
        </div>
    </td>
</tr>
</table>
</div>
</div>

I have a MVC controller for Business. The Business View is using the _Layout.cshtml and is not a partial view. So, it almost works but each click on a tab reloads the whole page and it resets the active tab to the first one, even when I clicked on the second one.

One thing I thought could work is if I added an extra script to the Index.cshtml of the Business view using a helper. This inserts this script to select the Business tab in the head of the page. As discussed in Using sections in Editor/Display templates

@Html.Script(
@<script src="@Url.Content("~/tuxmap/scripts/tabSelectBusinesses.js")" type="text/javascript"></script>)

But, the downside of that is that when I click to the Business tab and then on the map, the URL switches to localhost:XXXX/Business and stays there instead of localhost:XXXX. The map toggles okay but the whole thing is funky.

What's the right solution ?

Community
  • 1
  • 1
bluesky
  • 150
  • 1
  • 1
  • 11

1 Answers1

1

Your problem is that you use actionlink, that will always link to a new page. Try to do this instead:

In UL

<li><a href="#business">business</a></li>

Container for tab

<div id="business">
   @{
        Html.RenderAction("Index", "Business", new{ Id = Model.Id});
    }
</div>

this will render a partialview from index in business controller

Controller

public PartialViewResult(int Id){
    return PartialView(...) and so on
}

Ajax update div

@using Ajax.BeginForm("action","controller", null, new AjaxOptions{UpdateTargetId = "yourDivIdToPutResultIn"}, new{id="frmEdit"}){
    <input type="hidden" value="@Model.PutInIdHere" name="Id" id="editId"/>
}

**JS script to fire edit from button

$('#buttonId').click(function(){ 
    $('#editId').val('theIdYouWantToSend');
    $('#frmEdit').submit();
});

Also watch the vids from http://www.asp.net/mvc on the right side from pluralsight.

This answer will make the tabs work but really, its cleaner to avoid jquery tabs with MVC

bluesky
  • 150
  • 1
  • 1
  • 11
thsorens
  • 1,308
  • 12
  • 21
  • thanks that was actually very helpful. There is a bit more to it which I am trying to figure out. First thing it's: Html.RenderAction("Index", "Business"); and I had to set Layout=null in the index.cshtml for the Business to make it a partial view. Trying to make tabs swap the divs right now... update in a bit... – bluesky Apr 15 '14 at 17:15
  • Ok, that got me a step closer and I think I see how that can work out. Much thanks. I got the tabs going back and forth. However, I have some CRUD links in the index table that is displayed. Those are currently rendering in the @RenderBody() section which I left under the whole tabbed area. I'd really like to be able to click on say the "Edit" link and replace the Index table with the Edit.cshtml output. So, basically, it would still be on the Business tab but the Index.cshtml would be replaced with the Edit.cshtml. – bluesky Apr 15 '14 at 17:47
  • The Index.cshtml has links which are reloading the page - also they pass in the id number for the row selected - How do I fix this ? Any help is deeply appreciated. @Html.ActionLink("Edit", "Edit", new { id = item.Id })
    @Html.ActionLink("Details", "Details", new { id = item.Id })
    @Html.ActionLink("Delete", "Delete", new { id = item.Id })
    – bluesky Apr 15 '14 at 17:50
  • Sending the ID is pretty simple, just add a new inside the RenderAction, and define the variables you want to send in. – thsorens Apr 15 '14 at 18:42
  • Well, is there any reason why you put the tabs in the masterpage? It would probably be easier to have it in an ActionResult, based on the masterpage. Anyways, you could always do some ajax-magic filling the div in the tab. I'll post an example of that for you – thsorens Apr 15 '14 at 18:47
  • but changing those CRUD links in index.cshtml to a renderaction will draw the view within the cell of the index table... i.e. I'd like the Edit link to replace the entire Index table within the tab when it is clicked. – bluesky Apr 15 '14 at 18:51
  • I added some new stuff that shows how you can change the content of the table using ajax. Remember to include the jquery unobtrousive script – thsorens Apr 15 '14 at 18:55
  • oh, just noticed your last, yes, I could really use an example. Thank you! – bluesky Apr 15 '14 at 18:56
  • Your idea to get rid of the tabs may be better. These changes ripple through all the autogenerated View code that I liked about MVC (there are a lot of tables/views that would need changes). I don't have a masterpage (ASP.NET MVC 5). I have a _ViewStart.cshtml and a _Layout.cshtml. What am I missing ? – bluesky Apr 15 '14 at 19:32
  • It would probably all work if I could stuff the map html into the MVC framework somehow and just had some buttons that called ActionLink which would fill in RenderBody(). How can I make a button call a view (i.e. map.cshtml) and render it naturally in RenderBody ? (Does there have to be a controller ?) That would work for me I think. Thanks again – bluesky Apr 15 '14 at 19:44
  • RenderBody() renders everything from your actionresult, so when you call Home/Index, it will render whatevers inside the HomeController, within the Index action. If you want a crash course in MVC, i'd suggest this [site](http://www.asp.net/mvc), check out the tutorial on the right side which is made by pluralsight (Scott Allen). It'll give you a great basis on everything. – thsorens Apr 15 '14 at 19:58
  • I took a look at a new project and everything you said is making sense. The cleanest way is to rebuild the site based on the sample MVC app that starts up. You got my tabs working but I realize now how it goes against this architecture. I'm checking out the vids too. Thanks again ! – bluesky Apr 15 '14 at 20:11