I am using Jquery Datatable in asp.net MVC. I bind the sql query in datatable and the datatable i am using one Editfor, here i am using datetime picker. i attached my code on below. please help me.
<table id="datatable" class="display" border="1" cellpadding="2" cellspacing="2">
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.RMAKey)
</td>
<td>
#Html.DisplayFor(modelItem => item.SalesPerson)
</td>
<td>
#Html.DisplayFor(modelItem => item.SalesRep)
</td>
<td>
#Html.DisplayFor(modelItem => item.Datecreated)
</td>
<td>
#Html.EditorFor(model => model.First().FollowupDate)
#Html.ValidationMessageFor(model => model.First().FollowupDate)
</td>
<td>
#Ajax.ActionLink("Followup", "FollowupUpdate", new { RMAKey = item.RMAKey, FollowupDate=item.FollowupDate }, new AjaxOptions { UpdateTargetId = "modal", InsertionMode = InsertionMode.Replace, HttpMethod = "GET", OnSuccess = "onModalLoad" }, new { #class = "icon-secondary note" })
</td>
</tr>
}
</tbody>
</table>
The above code i am using #Html.EditorFor(model => model.First().FollowupDate). This is my problem occur here i get the date and send the date to partial view. How to generate the date dinamically?
Related
I have created a very simple search form in my MVC application which allows the user to search for a string term and returnt he results. I would like to let my users search between two dates but I'm not sure how I'd program that.
Here is my code so far. As you'll notice this is only for searching for a string that is passed into the controller as a parameter. I have started writing the code for the date pickers but I've gotten stuck.
View
<h2>Search</h2>
#using (Ajax.BeginForm("Search", "Search", new AjaxOptions
{
OnSuccess = "Success",
OnFailure = "Failure",
UpdateTargetId = "test",
InsertionMode = InsertionMode.Replace
}))
{
<table class="table">
<tr>
<td><label>Vessel Name</label></td>
<td>#Html.TextBox("term",null, new { #class = "k-textbox" })</td>
</tr>
<tr>
<td>
<label>From</label>
#(Html.Kendo().DatePicker()
.Name("date_from")
.Value(DateTime.Today)
)
</td>
<td>
<label>To</label>
#(Html.Kendo().DatePicker()
.Name("date_to")
.Value(DateTime.Today)
)
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Search" />
</td>
</tr>
</table>
}
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Location</th>
<th>MMSI</th>
<th>Created</th>
</tr>
</thead>
<tbody id="test">
#Html.Partial("_results")
</tbody>
</table>
Controller
public ActionResult Search(string term, DateTime date_from, DateTime date_to)
{
var vessels = (from o in db.vessels
select o);
if (!String.IsNullOrEmpty(term))
{
vessels = vessels.Where(s =>
s.vessel_name.Contains(term) ||
s.vessel_location.Contains(term) ||
s.vessel_mmsi.Contains(term));
}
return PartialView("_results", vessels);
}
Partial View
Since I'm using ajax to update a target ID I return the results as in a partial view using the 'replace' insertion method.
#model IEnumerable<Multiple_Table_Post.Models.vessel>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.vessel_name)
</td>
<td>
#Html.DisplayFor(modelItem => item.vessel_location)
</td>
<td>
#Html.DisplayFor(modelItem => item.vessel_mmsi)
</td>
<td>
#Html.DisplayFor(modelItem => item.created)
</td>
</tr>
}
As you'll notice I have started putting the date pickers in place but I don't know how to write the c# to allow me to search between them. I'm passing them into the method as datetime parameters but after is where I'm totally stuck.
Many thanks
You could do this in either of the LINQ queries you have shown.
var vessels= (from o in db.vessels
where (o.created>= date_from && o.created<= date_to)
select o);//I have not tested this.
More Info:
C# Linq Where Date Between 2 Dates
You can also filter the dates in the second LINQ query as seen in the other answers
var vessels = (from o in db.vessels
where o.vessel_date => date_from
&& o.vessel_date =< date_to
&& ( term==null
|| o.vessel_name.Contains(term)
&& o.vessel_location.Contains(term)
&& o.vessel_mmsi.Contains(term))
select o);
I have a view with three textboxes and a CheckBox:
#foreach( var item in Model )
{
<tr>
<td>
#Html.CheckBox("name")
</td>
<td>
#Html.EditorFor(m => item.Toolbox)
</td>
<td>
#Html.EditorFor(m => item.Name)
</td>
<td>
#Html.EditorFor(m => item.Link)
</td>
</tr>
}
And how i can create JS code to bind this checkboxes to diffirent textboxes? This "name" check box should disable/enable three textboxes for every time loop creates it. There are 500+ items in Model.
You may set counter and create unique class or some attribute for each block. For example:
<table id="myTable">
#{
int c = 0;
for(int i = 0; i < Model.Count; i++)
{
c++;
<tr class="#("block"+c)">
<td>
#Html.CheckBox("name", new {data_block=c})
</td>
<td>
#Html.EditorFor(m => item.Toolbox)
</td>
<td>
#Html.EditorFor(m => item.Name)
</td>
<td>
#Html.EditorFor(m => item.Link)
</td>
</tr>
}
</table>
and then javascript code:
$(function(){
$('#myTable checkboxes').click(function(){
var checkbox = $(this),
blockNum = checkbox.data('block'),
inputs = $('tr.block'+blockNum),
state = checkbox.is(':checked');
$.each( inputs, function( i, el ){
$(el).prop("disabled", state);
});
});
});
Another way
<table id="myTable">
#foreach( var item in Model )
{
<tr>
<td>
#Html.CheckBox("name", new { #class = "nameCheckbox" })
</td>
<td>
#Html.TextBoxFor(m => item.Toolbox, new { #class = "someCleverClassName" })
</td>
<td>
#Html.TextBoxFor(m => item.Name, new { #class="someCleverClassName" })
</td>
<td>
#Html.TextBoxFor(m => item.Link, new { #class="someCleverClassName" })
</td>
</tr>
}
</table>
And the javascript
$(function(){
$('#myTable .nameCheckbox').click(function(){
$(this).closest('tr').find('.someCleverClassName').prop("disabled", $(this).prop('checked'));
});
});
I have a following problem: I have form, which has some dropdowns, textareas etc. and also I have two tables:
- SelectedItems
- AvailableItems
This two tables are a result of rendering two lists in my Model. What I need to do is when user press add button in a defined item row then I move item from AvailableItems to SelectedItems. My idea was to operate on lists so add to one, remove from another. I have no idea how to update lists of objects which can be found in model
public class Model
{
public List<Item> SelectedItems {get; set;}
public List<Item> AvailableItems {get; set;}
//other properties
}
My Partial View is:
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th></th>
</tr>
</thead>
#if (Model.SelectedItems != null)
{
foreach (var item in Model.SelectedItems)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.ID)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
<td>
#Html.ActionLink("Add", "RemoveItem", new { ItemID = item.ID}, new { #class = "btn btn-primary" })
</td>
</tr>
}
}
</table>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th></th>
</tr>
</thead>
#if (Model.AvailableItems != null)
{
foreach (var item in Model.AvailableItems)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.ID)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
<td>
#Html.ActionLink("Add", "AddItem", new { ItemID = item.ID}, new { #class = "btn btn-primary" })
</td>
</tr>
}
}
</table>
Problem is that I can't reload whole page. I need to make inserts do db basing on that form so is there any possibility to update model and then start inserts?
Thank you for your help.
I have the following view , which mainly view a list of items as a displayfor:-
#foreach (var item in Model.Resources) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.SystemInfo.MODEL)
</td>
<td>
#Html.DisplayFor(modelItem => item.RESOURCENAME)
</td>
<td>
#Html.DisplayFor(modelItem => item.ResourceLocation.SiteDefinition.AccountDefinition.SDOrganization.NAME)
</td>
<td>
#Html.DisplayFor(modelItem => item.ResourceLocation.SiteDefinition.SDOrganization.NAME)
</td>
<td>
#Html.DisplayFor(modelItem => item.ComponentDefinition.COMPONENTNAME)
</td>
<td>
#Html.DisplayFor(modelItem => item.ResourceState.STATEDESC)
</td>
<td id = "#item.RESOURCEID">
#using (Ajax.BeginForm("CreateOn","VirtualMachine", new AjaxOptions {
InsertionMode = InsertionMode.Replace,
UpdateTargetId = item.RESOURCEID.ToString() ,
LoadingElementId = "progress",
HttpMethod = "POST"})){
<span class="f">Hypervisor Server</span>
#Html.DropDownListFor(model =>model.VirtualMachine.ServerID, ((IEnumerable<t.Models.Server>)ViewBag.Servers).Select(option => new SelectListItem {
Text = (option == null ? "None" : option.Technology.Tag),
Value = option.ServerID.ToString(),
Selected = (Model != null) && (Model.VirtualMachine != null) && (option.ServerID == Model.VirtualMachine.ServerID)
}), "Choose...")
#Html.ValidationMessageFor(model =>model.VirtualMachine.ServerID)
#Html.Hidden("IT360id", item.RESOURCEID)
#Html.Hidden("CustomerName",item.ResourceLocation.SiteDefinition.AccountDefinition.SDOrganization.NAME)
#Html.Hidden("SiteName",item.ResourceLocation.SiteDefinition.SDOrganization.NAME)
#Html.Hidden("ResourceName",item.RESOURCENAME)
<input type="submit" value="Add To" class="btn btn-primary"/>
}
But the user can read the data and chose to create the item on our database, using the ajax.beginform. But to do so I need to pass some values of the DisplayFor to the model binder. Currently I have added all the needed data as a hiddenfields, and then I will pass these values to my action method as follow:-
[HttpPost]
public ActionResult CreateOn(VirtualMachineOnIT360Only vm, long IT360id, string CustomerName, string SiteName, string ResourceName)
{
I found that my approach is not very reliable and I am trying to figure out a more reliable solution. So can anyone advice please?
Thanks
To post DisplayFor back, I prefer to use hiddenfor instead of hidden like this:
#Html.HiddenFor(item => item.RESOURCENAME);
This ajax call never creates the alert on success even though it has reached and returned success = true on the server side method.
#model IEnumerable<Test.Models.Task>
#Styles.Render("~/Content/Site.css")
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<div id ="alerts">
#Html.Action("_Tasks")
<script type="text/javascript">
$(document).ready(function poll() {
$.ajax({
type: 'GET',
cache: false,
url: '#Url.Action("TasksRefresh")',
dataType: "json",
complete: function () { setTimeout(poll, 10000); },
success: function (data) {
alert("Testing")
}
});
})();
</script>
#* <script type="text/javascript">
var alerts = '#ViewBag.Alerts';
#foreach (var i in alerts)
{
}
</script>*#
</div>
<table>
<tr>
<th>Category</th>
<th>Severity</th>
<th>Assigned to Role</th>
<th>Assigned To</th>
<th>Chart #</th>
<th>Note</th>
<th>Alert</th>
<th>Status</th>
<th>Creator By</th>
<th>Create Date</th>
<th>Due Date</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.LookupTaskCategory.CategoryName)
</td>
<td>
#Html.DisplayFor(modelItem => item.LookupTaskSeverity.SeverityName)
</td>
<td>
#Html.DisplayFor(modelItem => item.AssignedToRoleName)
</td>
<td>
#Html.DisplayFor(modelItem => item.AssignedToName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Patient.ChartNo)
</td>
<td>
#Html.DisplayFor(modelItem => item.Note)
</td>
<td>
#Html.DisplayFor(modelItem => item.AlertFlag)
</td>
<td>
#Html.DisplayFor(modelItem => item.LookupTaskStatu.StatusName )
</td>
<td>
#Html.DisplayFor(modelItem => item.CreatedByName)
</td>
<td>
#Html.DisplayFor(modelItem => item.CreatedOnDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.DueDate)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
#Html.ActionLink("Details", "Details", new { id=item.Id }) |
#Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
This is the server side method in my controller. I have tried to replace JsonResult with ActionResult, but it didn't change the outcome.
public JsonResult TasksRefresh()
{
//Testing to see if this return ever gets received by ajax.
return Json(new { success = true });
}
You're getting an exception on the server - try debugging the .NET code, or watching your response with the browser tools, to see it.
If you want to return a JSON object on a GET method, you need to include a JsonRequestBehavior parameter to the Json call, like:
return Json(new { success = true }, JsonRequestBehavior.AllowGet);
EDIT
Actually, it looks like you can't see it if you debug on the server - you'd have to see it in the response. Apparently the exception is thrown further down after the Json method.