Refresh User Control in MVC - asp.net-mvc

I have 3 user controls in an aspx page in my MVC application.
i bind data from DB from one controller.
on the selection of "Filter / Status" i want to bind data (refresh) to "List" user control with out refreshing "Filter & Status" user controls.
below are my user controls in aspx page.
please help me how to refresh part of the page/user control.
i have tried by returning only "ListView" View data. but its searching for the other 2 views & throwing exception.
<td>
<%Html.RenderPartial("Filter", ViewData["FilterView"]); %>
</td>
<td valign="top" width="15%">
<%Html.RenderPartial("Status", this.ViewData["StatusView"]); %>
</td>
<td valign="top" width="85%">
<%Html.RenderPartial("List", this.ViewData["ListingView"]); %>
</td>

do sth like this
html (aspx page):
<div id="ListingView">
<%Html.RenderPartial("List", this.ViewData["ListingView"]); %>
</div>
...
<input type="button" id="refreshListingView" />
javascript to handle it:
$('#refreshListingView').click(function () {
var selectedStatusId = 'get here proper status id';
$.ajax({
url: '/YourController/GetListingView',
type: "GET",
data: { "statusId": selectedStatusId },
success: function (response) {
$('#ListingView').html(response);
}
});
});
YourController:
[HttpGet]
public ActionResult GetListingView(int statusId)
{
ViewData["ListingView"] = your data filtered by statusId or what you want;
return PartialView("List",ViewData["ListingView"]);
}
Instead of ViewData["ListingView"] I would use dedicated model, but it's just an example. It should work.

Related

How to Pass Data to a vuestrap Modal component

Im trying to pass some table data to its child vuestrap Modal component. The modal will be reused by all the Td's where the checkbox is calling the modal.
<div id="ordertbl" class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
...
</tr>
</thead>
<tbody>
<tr v-repeat="slides">
<td>#{ {NAME}}</td>
<td>#{ {MESSAGE}}</td>
<td> <input type="checkbox" v-on="click:showMod = true" v-model="published" > </td>
</tr>
</tbody>
</table>
<modal title="Modal" show="#{{#showMod}}" effect="fade" width="400">
<div class="modal-body">You will publish NAME, MESSAGE</div>
</modal>
</div>
when the checkbox is clicked. As you can see every row in the table has one checkbox so the data to be passed to the Modal will be unique to its row.
As Im new to Vue, besides Im trying to use Vuestrap to not reinvent things,
I dont know how to give that Data to the Modal when it pops.
new Vue({
el:'#ordertbl',
components: {
'modal':VueStrap.modal
},
data: {
showMod: false,
sortKey: '',
reverse:false,
slides: {
id: '',
name: '',
message: '',
published: ''
}
},
Basically I want to do the following
$('#exampleModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) // Button that triggered the modal
var recipient = button.data('whatever') // Extract info from data-* attributes
// If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
// Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
var modal = $(this)
modal.find('.modal-title').text('New message to ' + recipient)
modal.find('.modal-body input').val(recipient)
})
Pass the related data to the Modal, but with Vuestrap
You can use v-for="slide in slides". So every tr can get one of the slide object. Then you can pass it as a props to modal.
Extra ajax request is not required.
I manage to solve it with an ajax request depending on the Id clicked.

<fieldset hides when page re-loads - MVC 5

EDIT:
Here is the exact problem that I have demonstrate, please have a look and as soon as I click the submit button it post back and lost its state and as you can see in the sample code I have three pages I'm posting the form
1) EmployeeForm, 2) EmployerForm, 3) ContractorForm
https://dotnetfiddle.net/wVtwgW
How do I persist the checkbox?
Once I post the page and it reloads the same page if I have my data-model invalid and it display the error message on the screen but the problem is that, it hides the fieldset and the user has to click the checkbox again to show the fieldset.
my question is: how can I still show the fieldset and show the error message in it?
//my scripts that shows the fieldset
<script>
$(document).ready(function() {
$('#Employee').change(function() {
if (this.checked) {
$('#emp').show();
}
});
});
</script>
//it shows the fieldset with checkbox:
<fieldset class="fieldset-auto-width">
<legend>
Select Employee
</legend>
<table width="auto">
<tr>
<th>
Employee
</th>
<td>
#Html.CheckBox("Employee")
</td>
</tr>
</table>
</fieldset>
//my form where I have all the input text and button etc...
<fieldset id="emp" style="display: none" class="fieldset-auto-width">
<legend>
Employee Display
</legend>
#using (Html.BeginForm("EmployeeServer", "EmployeeForm", FormMethod.Post))
{
#Html.ValidationSummary(true)
<div>..... </div>
}
</fieldset>
Instead of using #Html.CheckBox() use #Html.CheckBoxFor()
<td>
#Html.CheckBoxFor(m => m.Employee)
</td>
this will retain the state of the checkbox when you return the model state errors..
in your javascript, just call the $("#Employee") change event after the page loads..
$(document).ready(function() {
$('#Employee').change(function() {
if (this.checked) {
$('#emp').show();
}
});
$('#Employee').trigger("change");
});
Set the Fieldset display value when view is rendered
<fieldset id="emp" style="#(Model.Employee ? "": "display: none")" class="fieldset-auto-width">
<legend>
Employee Display
</legend>
#using (Html.BeginForm("EmployeeServer", "EmployeeForm", FormMethod.Post))
{
#Html.ValidationSummary(true)
<div>..... </div>
}
</fieldset>
this will hide the fieldset if Model.Employee = false or display it if Model.Employee = true.
Just do it with JavaScript. Look for the rendered element from the validation summary helper, and if it exists then you can show your employee form. You can add it to your already executing script like this:
$(document).ready(function() {
$('#Employee').change(function() {
if (this.checked) {
$('#emp').show();
}
});
//check for validation summary elements
if($('.validation-summary-errors').length > 0){
//and show previous entry if present
$('#emp').show();
}
});

Why partial view not rendering in the same page? (ASP.NET MVC 4)

I've faced a problem for partial view displaying.
//This is Controller Action
//_AcademicInfo is the Partial view name
public PartialViewResult GetAcademicInfo(int empId)
{
var acad = _academicService.GetAcademinByEmp(empId);
return PartialView("_AcademicInfo", acad);
}
<!--Parent/caller page cshtml code-->
#Ajax.ActionLink(
"Academic Details",
"GetAcademicInfo",
"Employee", new {empId = Model.Id},
new AjaxOptions {UpdateTargetId = "AcademicDetails", InsertionMode = InsertionMode.InsertAfter}
)
<!--This is partial view cshtml-->
#model IEnumerable<Hik.PhoneBook.Data.Entities.Academic>
<div id="result">
<table>
<tr>
<th>Degree Name</th>
<th>Passing Year</th>
<th>CGPA</th>
<th>Institute</th>
</tr>
#foreach (var acad in Model)
{
<tr>
<td>#Html.DisplayFor(m => acad.DegreeName)</td>
<td>#Html.DisplayFor(m => acad.PassingYear)</td>
<td>#Html.DisplayFor(m => acad.CGPA)</td>
<td>#Html.DisplayFor(m => acad.Institute)</td>
</tr>
}
</table>
</div>
As because, I need to get the partial view by a Link clicking, not by rendering directly like #Html.Partial("_AcademicInfo", Model.Academic). Thats why I've used Ajax.ActionLink. Whenever I click on "Academic Details" link, its executing the accurate result. But unfortunately its not displaying in the same page. Its going to appear in another page. (Its MVC 4)
What should I need to change to render the partial view in the same page?
If you are redirecting to a new page rather than staying on the same page, it means that jquery.unobtrusive-ajax.js is not loaded and #Ajax.ActionLink() falls back to a normal link.
Either you have either
not included the script
have the scripts in the wrong order (jquery{version}.js must come
first)
You have duplicate scripts
Maybe you can use jquery's .load() function ? Like this.
$("divId").load('#url.action("getacademicInfo")', {empId : 4});
https://api.jquery.com/load/
Try using JQuery Ajax to load the partial view into the Div on the page on button/link click instead.
var jsondata = { Id: iddata };
$.ajax({
url: "/Controllername/GetAcademicInfo",
method: "POST",
dataType: "html",
data: jsondata,
success: function (response) {
$("#divId").html(response);
}
});
Please note that this is just a sample code for giving you an idea.
Hope that helps!

jeditable table cell

I am having an issue when using jeditable to edit a cell in a table.
The project is the MVC 2 web application and the table has been put on the standard about page.
How do i tell the script to call a specific method in the controller ? because it is currently just loading the entire page into the cell.
This is the javascript:
$(document).ready(function () {
$('.editable').editable('http://localhost:2196/Home/About', {
type: 'text',
cancel: 'Cancel',
event: 'dblclick',
submit: 'OK',
tooltip: 'double Click to edit...'
});
});
This is the table :
<% foreach (DataTableEditable.Models.Company item in (IEnumerable<DataTableEditable.Models.Company>)Model)
{%>
<tr id="<%= Html.Encode(item.ID) %>">
<td class="editable"><%= Html.Encode(item.Name) %> </td>
<td><%= Html.Encode(item.Address) %> </td>
<td><%= Html.Encode(item.Town) %> </td>
</tr>
<% }%>
Thanks lots
John
Thanks but I have now solved it.
I needed to add
[AcceptVerbs("POST")]
public ActionResult UpdateSettings(string id, string value)
{
return Content(value);
}
to the controller.

CSS not being applied with jQuery UI dialog

I have an MVC view with a Ajax.ActionLink.
this.Ajax.ActionLink( "Create Offender", "Create", "ReportOffender",
null, new AjaxOptions()
{
HttpMethod = "GET", UpdateTargetId = "dialog",
OnBegin = "function() { $('#dialog').dialog('open'); }"
},
new Dictionary<string, object>() { { "class", "optionlink" } } )
This calls an action called "Create" which returns a partial view that is then placed inside of a div/dialog. The contents of this partial view is a table, some rows/columns and textboxes each of which have a CSS class assigned to them.
The problem is when the dialog is shown, with the partial view rendered inside, the CSS classes are ignored and not rendered.
Any ideas as to why this is happening?
Thanks
Michael
P.S This is the partial view that is returned from the Create GET action (see the Ajax.ActionLink above)...
<% using( this.Ajax.BeginForm( "Create", "ReportOffender", null, new AjaxOptions() { HttpMethod = "POST", UpdateTargetId = "dialog" } ) ) { %>
<% this.Html.RenderPartial( "~/Views/IncidentReporting/Report/Offender/DataEntryUserControl.ascx", this.Model ); %>
<% } %>
Once the user completes the form, snippet below, the Create POST method is called and the response is placed into the same dialog. This response will either be a "you've got errors" or a "saved successful" message. I use another RenderParital here because I also have a similar Edit version that uses the same form.
Below is a snippet of the HTML inside of the DataEntryUserControl.ascx and represents the form the user has to complete...
<table class="entity-form">
<tr>
<td class="entity-form-validation-summary" colspan="2">
<%= this.Html.ValidationSummary() %>
</td>
</tr>
<!-- FIRST NAME -->
<tr>
<td class="entity-form-caption"><label for="Entity.FirstName">First Name</label></td>
<td>
<%= this.Html.TextBox( "Entity.FirstName", this.Model.Entity.FirstName, 50, 50, this.Page.User.IsInRoles( SecurityGroups.Translators ), null ) %>
<%= this.Html.ValidationMessage( "Entity.FirstName" ) %>
</td>
</tr>
<!-- LAST NAME -->
<tr>
<td class="entity-form-caption"><label for="Entity.LastName">Last Name</label></td>
<td>
<%= this.Html.TextBox( "Entity.LastName", this.Model.Entity.LastName, 50, 50, this.Page.User.IsInRoles( SecurityGroups.Translators ), null )%>
<%= this.Html.ValidationMessage( "Entity.LastName" )%>
</td>
</tr>
As you can see there is nothing special. The TextBox methods with the extra parameters are extension methods I created to add MaxLength, Size, etc, to the textbox controls. It is here were I'm having the problems with the CSS. I use the same CSS classes, and form structure, elsewhere in "normal" views and they rendered correctly.
verify your html output/ or look table with web developer or firebug/
may be it hasnt class in first place/
if situation isnt like that;
put stylesheet link into your ascx/

Resources