How to fill an empty <div> with a partial view using ajax - ajax

I am using Visual Studio 2013, .Net 4.5, MVC and Visual Basic. I am trying to render a partial view in an empty div of another view using jQuery and Ajax. My script is running successfully in that on the drop down list change, my partial view is rendered, but instead of it rendering in a div on the same view, it renders on it's own page.
Main View:
<div class="col-md-8">
<div class="btn-group">
<button class="btn btn-default dropdown-toggle btn-sm" type="button" id="dropDownSelectBuilding" data-toggle="dropdown"
aria-expanded="true">
<span class="selection"></span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" aria-labeledby="dropDownSelectBuilding">
#For Each item In Model
Dim currentItem = item
#<li role="presentation"><a role="menuitem" tabindex="-1" href="/LocationSelect/SelectFloor"
>#Html.DisplayFor(Function(modelItem) currentItem)</a></li>
Next
</ul>
</div>
#section Scripts
<script>
$('#dropDownSelectBuilding').onChange(function () {
$.ajax({
url: '/LocationSelect/SelectFloor',
contentType: 'application/html; charset=utf-8',
type: 'GET',
dataType: 'html'
})
.success(function (result) {
$('#showFloors').html(result);
})
.error(function (xhr, status) {
alert(status);
})
});
</script>
End Section
</div>
</div>
<div class="row" id="showFloors">
</div>
Partial View:
<div class="col-md-4">
<label>*Select the floor this item will move to:</label>
</div>
<div class="col-md-8">
<div class="btn-group">
<button class="btn btn-default dropdown-toggle btn-sm" type="button" id="dropDownSelectFloorMoveTo" data-toggle="dropdown" aria-expanded="true">
<span class="selection"></span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" aria-labeledby="dropDownSelectFloorMoveTo">
#For Each item In Model
Dim currentItem = item
#<li role="presentation"><a role="menuitem" tabindex="-1" href="/LocationSelect/SelectFloor"
>#Html.DisplayFor(Function(modelItem) currentItem)</a>
</li>
Next
</ul>
</div>
</div>
Controller:
Public Class LocationSelectController
Inherits Controller
Private db As New MCSCInventoryModels
' GET: LocationSelect
Function Index() As ActionResult
Dim distinctBuildings = From Row In db.tblLocations
Select Row.Building
Distinct
Return View(distinctBuildings)
End Function
' GET: FloorS
Function SelectFloor(ByVal selectedBuilding As String) As PartialViewResult
Dim floorsInSelectedBuilding = From Row In db.tblLocations
Where Row.Building = selectedBuilding
Select Row.Floor
Distinct
Return PartialView(floorsInSelectedBuilding)
End Function
End Class
Can you tell me why this ajax call opens the partial view in it's own page, instead of rendering it in the specified div?
Update I have checked the accepted answers to both of these questions and they have not solved the issue.
Here
and
Here

You are most likely missing the unobtrusive script in your layout page.
<script src="/Scripts/jquery.unobtrusive-ajax.js"></script>
refer to this question:
Why does Partial View show as full page in MVC 5 Visual Studio 13?
one more thing is to add the last (UnobtrusiveJavaScriptEnabled) line to your config file (Web.Config):
<configuration>
<appSettings>
<add key="webpages:Version" value="2.0.0.0"/>
<add key="webpages:Enabled" value="false"/>
<add key="PreserveLoginUrl" value="true"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>

Ultimately what was causing this issue was my use of a non-traditional drop down. To solve the problem I added a class to the <a role="menuitem"> like so:
#<li role="presentation"><a class="ddlist" role="menuitem" tabindex="-1" href="#"
>#Html.DisplayFor(Function(modelItem) currentItem)</a></li>
and then used:
$('.ddlist').click(function () {
to trigger the ajax call.
I also removed the href="/LocationSelect/SelectFloor" which was left over from the design phase and causing the partial view to open in it's own page when a selection was made and the ajax wasn't being triggered.

Related

Implement search from inside layout in asp.net core mvc

I'm creating my first web app using asp.net core mvc, so I'm stuck with implementing global search using textbox inside layout, I can't find where to implement the search after user press Enter, Is it implemented in controller or something else, and how to implement it...
I'm stuck with implementing global search using textbox inside layout, I can't find where to implement the search after user press Enter
To implement the above requirement, you can refer to the following code snippet.
Adding form with input for searching functionality in _Layout.cshtml
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</li>
<li class="nav-item">
<form id="f_search" method="get" asp-action="Search">
<div class="form-group">
<div class="input-group">
<input id="ipt_search" type="search" class="form-control" name="SearchTerm" />
<span class="input-group-btn">
<button class="btn btn-default">
Search
</button>
</span>
</div>
</div>
</form>
</li>
</ul>
</div>
JS code of checking keydown event and user input
<script>
$(function () {
$('#f_search input').keydown(function (e) {
var searchTerm = $("#ipt_search").val();
if (searchTerm == "" && event.keyCode === 13) {
e.preventDefault();
return false;
}
});
})
</script>
Action method of Search
public IActionResult Search(string SearchTerm)
{
//...
//do query based on SearchTerm
//code logic here
//...
return View();
}
Test Result

CodeIgniter Ajax does not populate content in modal form

The modal form called does not load the content using Ajax. So I have a modal dialog that is supposed to come up with a dropdown and Cancel & OK buttons. The dropdown is loaded from database, however it comes up empty.
I've tried to look this up but examples are either close or very far but none offered something similar.
I've also tried different approaches but still I end up with the same issue.
View:
foreach($aRecords as $record)
...
<a href="#"
class="btn btn-primary btn-xs change_parent_class"
data-toggle="modal"
data-target="#confirmChangeParent"
id ="<?php echo $record->id; ?>"
><i class="fa fa-arrow-up"></i> Change Parent</a>
The modal
<div class="modal fade" id="confirmChangeParent" role="dialog" aria-labelledby="confirmChangeParent" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Change Parent</h4>
</div>
<div class="modal-body">
<p>Please select one of the following options</p>
<div id="areaChangeParent" name="areaChangeParent"></div>
</div>
<div class="modal-footer">
<a href="#" class="btn btn-info btn-xs default" data-dismiss="modal">
<i class="fa fa-reply"></i> Cancel
</a>
<a href="#" class="btn btn-success btn-xs default" id ="confirmSubmitDelete">
<i class="fa fa-arrow-up"></i> Move
</a>
</div>
</div>
</div>
</div>
JavaScript
$(document).ready(function(){
//function change_parent(id)
$(".change_parent_class").click(function()
{
id = $(this).attr('id');
alert("p-aci:" + id);
e.preventDefault();
$.ajax({
url: "<?php echo base_url() ?>project/project_issue/get_parents/",
method: "POST",
data: {mid:id},
success: function(data)
{
$("#areaChangeParent").html(data);
}
});
});
});
Controller function to fetch data is in a dummy state just for testing:
function get_parents()
{
echo "bla";
return "bla bla";
}
No matter what the prompt is displayed but empty.
You are using e.preventDefault(); without function(e)

hide buttons in web forms app if being page has two user control refs

I am working on a web forms app where one of my user controls is used on multiple pages. Is there a simple if/else statement that can hide a div if not referenced to a specific page. For instance, I have a button group that is showing in both page A and B but I only need it to show on page B if that page is active. Any help will be appreciated.
Here is the button group I wish to have only shown on one user control:
<asp:Label ID="lblQty" runat="server" AssociatedControlID="txtQuantity">Quantity</asp:Label>
<div class="row">
<div class="col-xs-12 col-md-6">
<div class="input-group mb-3">
<div class="input-group-prepend">
<button type="button" class="btn btn-primary" onclick="changeQuantity('minus');" id="minus-btn"><i class="fa fa-minus"></i></button>
</div>
<asp:TextBox ID="txtQuantity" cssClass="form-control form-control-sm" runat="server" value="1" min="1" max= ClientIDMode="Static"></asp:TextBox>
<div class="input-group-prepend">
<button type="button" class="btn btn-primary" onclick="changeQuantity('plus');" id="plus-btn"><i class="fa fa-plus"></i></button>
</div>
</div>
</div>
</div>
This is the block that is showing on two different .ascx controls.
You can add a public property in the ascx control:
public string MyPage { get; set; }
and in a method (or in a PreRender event) you can use:
public void MyMethod()
{
MyButton.Visible = MyPage == "B";
}
You can set the property from page B.aspx
<%# Register TagPrefix="cc" TagName="MyControl"
Src="~/Controls/MyControl.ascx" %>
<cc:MyControl runat="server" Id="MyControlId" MyPage="B" />

Ajax not triggering for Partial view

I am adding Partial view in to my main view , the partial view code snippet is:
<li class="item-#j deeper parent active">
<a class="" href="">
<span data-toggle="collapse" data-parent="#menu-group-1" href="#sub-item-#j" class="sign"><i class="icon-plus icon-white"></i></span>
<span class="lbl">Brands</span>
</a>
<ul class="children nav-child unstyled small collapse" id="sub-item-#j">
#foreach (var brand in Model.BrandList)
{
<li class="item-#j deeper parent active">
<div class="checkbox">
<label>
<input type="checkbox" value="#brand._id">
#*<a class="" href="#Url.Action("FilterData", "ShopWindow", new {shopKey=house._id,houseKey=house._id,houseName = house.House})">*#
<span class="lbl">#brand.Brand</span>
#*</a>*#
</label>
</div>
</li>
j++;
}
</ul>
</li>
in my main view I have a script to get all the check boxes that user have selected, the script snippet :
<script type="text/javascript">
var selectedBrand=[];
$("input[type='checkbox']").change(function () {
$('#brand input:checked').each(function () {
selectedBrand.push($(this).attr('value'));
alert("Test");
})
sendAjaxRequest();
});
When I select any check box from the partial view the script's change function is not getting triggered. This change function triggers if I check any checkbox in the main page but not for checkboxes belongs to partial view .
Any idea?
Thanks
I found the solution :
changed
$("input[type='checkbox']").change(function () {
to
$(document).on('change', '.checkbox', function() {

How can i jump one cshtml page to another cshtml?

I have two .cshmtl pages used for registration. When user completes first registration page I want the controller to jump to another cshtml page without going to controller. How can I complete this task?
<div class="control-group form-actions pull-right">
<a href="~/Views/Registration/RegStep2" class="btn btn-primary">
Continue
<i class="fa fa-arrow-circle-right"></i>
</a>
</div>
When user click above continue button he will jump on another controller without controller...
Please see the below method using Partial views.
<div class="control-group form-actions pull-right">
<div id="first-div">
#Html.Partial("PartialView1")
</div>
<div id="second-div" style="display:none;">
#Html.Partial("PartialView2")
</div>
<a href="#" class="btn btn-primary" onclick="onclickContinue">
Continue
<i class="fa fa-arrow-circle-right"></i>
</a>
</div>
<script type="text/javascript">
function onclickContinue(e) {
$("#first-div").hide();
$("#second-div").show();
}
</script>

Resources