About validate in mvc3 - c#

This's my code in controller:
EApproveEntities db = new EApproveEntities();
DocumentDAO dataDocument = new DocumentDAO();
DocumentTypeData dataTypeDocument = new DocumentTypeData();
public void CreateDropDownListInDocument()
{
dataTypeDocument.GetDocumentType();
IEnumerable<SelectListItem> listTypeDocument = new SelectList(dataTypeDocument.documentType, "Key", "Value");
IEnumerable<SelectListItem> listPriorityDocument = new SelectList(dataDocument.Priority, "Key", "Value");
IEnumerable<SelectListItem> listStatusDocument = new SelectList(dataDocument.Status, "Key", "Value");
ViewBag.dataTypeDocument = listTypeDocument;
ViewBag.dataPriorityDocument = listPriorityDocument;
ViewBag.dataStatusDocument = listStatusDocument;
}
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult CreateDocument()
{
CreateDropDownListInDocument();
Document document = new Document();
return this.View(document);
//return this.View();
}
[ActionName("CreateDocument")]
[AcceptVerbs(HttpVerbs.Post)]
[Multiple_Button(Name = "Document", Value = "Cancel")]
public ActionResult Cancel()
{
return RedirectToAction("ListDocument");
}
//[ActionName("CreateDocument")]
[AcceptVerbs(HttpVerbs.Post)]
[Multiple_Button(Name = "Document", Value = "Create")]
public ActionResult CreateDocument(Document model,FormCollection formCollection)
{
CreateDropDownListInDocument();
return View(model);
}
This in code in View:
<table class="tablestyle">
<tr>
<td >
<div>
#Html.LabelFor(model => model.NameOfDocument)
</div>
</td>
<td>
#Html.TextBoxFor(model => model.NameOfDocument, new { #class = "input-textbox" })
#*#Html.ValidationMessageFor(model => model.NameOfDocument);*#
</td>
</tr>
<tr>
<td >
<div>
#Html.Label("Status")
</div>
</td>
<td>
#Html.DropDownListFor(model => model.Status,
(IEnumerable<SelectListItem>)#ViewBag.dataStatusDocument, new { #class = "combobox_style" })
</td>
</tr>
<tr>
<td >
<div>
#Html.Label(" Priority")
</div>
</td>
<td>
#Html.DropDownListFor(model => model.Priority,
(IEnumerable<SelectListItem>)#ViewBag.dataPriorityDocument, new { #class = "combobox_style" })
</td>
</tr>
<tr>
<td >
<div>
#Html.Label(" Type of Document")
</div>
</td>
<td>
#Html.DropDownListFor(model => model.DocumentType,
(IEnumerable<SelectListItem>)#ViewBag.dataTypeDocument, new { #class = "combobox_style" })
</td>
</tr>
<tr>
<td >
<div>
#Html.Label("Path forder")
</div>
</td>
<td>
#Html.TextBoxFor(model => model.PathFolder,new {#Type="file"})
#* #Html.ValidationMessageFor(model => model.PathFolder)*#
</td>
</tr>
<tr>
<td>
#Html.Label("Note")
</td>
<td>
#Html.TextAreaFor(model => model.Note, new { #class = "textarea_style" })
#*#Html.ValidationMessageFor(model => model.Note)*#
</td>
</tr>
<tr>
<td>
<div class="style_button">
<input type="submit" class="t_button" value="Create" name="Document"/>
</div>
</td>
<td>
<div class="style_button">
<input type="submit" class="t_button" value="Cancel" name="Document"/>
</div>
</td>
</tr>
</table>
I were close all validate . And i use the model of EntityFramwork. Don't have any validate.
But this's result:
I don't understand why it's check validate when i don't do anything.
Because this's take me some error.
Thanks!

Related

How to filter data with method GetAll() and UnitOfWork (Repository Pattern)

I have an initial table in the Index Get method where I query all the columns I want to display, with GetAll() method. Here I also load the drop-down menus where I select the parameters.
/********INDEX GET*********/
[HttpGet]
public IActionResult Index()
{
BudgetVM = new BudgetViewModel()
{
FBudget = new FBudget(),
YearsList = _unitOfWork.Budget.GetYearsListForDropdown(),
CompanyList = _unitOfWork.Budget.GetCompanyListForDropDown(),
CustomerList = _unitOfWork.Budget.GetCustomerListForDropDown(),
ProductGroupList = _unitOfWork.Budget.GetProductGroupListForDropDown(),
LicensingAreaList = _unitOfWork.Budget.GetLicensingAreaListForDropDown(),
PharmaFormList = _unitOfWork.Budget.GetPharmaFormListForDropDown(),
LedgerScenarioList = _unitOfWork.Budget.GetLedgerScenarioListForDropDown(),
CurrencyList = _unitOfWork.Budget.GetCurrencyListForDropdown(),
//Lista di tutti i record
RecordsList = _unitOfWork.Budget.GetAll(includeProperties: "ItemMaster,Customer")
};
return View(BudgetVM);
}
Here is the view that will send the parameters to the Index Post method:
#model SalesBudget.Models.ViewModels.BudgetViewModel
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<form method="post" asp-controller="Budget" asp-action="Index">
<div class="row">
<div class="col-6">
<br />
<h2 class="text-primary">Filter by</h2>
</div>
</div>
<div class="row">
<div class="col">
<div class="dropdown">
#*Parameter1: what I want to save in the db
Parameter2: actual list of items (dropdown menu)
Parameter3: default that I see in the dropdown
Parameter4: classes that I want to add*#
<!--BLOCCA IL PROGRAMMA IN DELETE-->
#Html.DropDownListFor(m => m.FBudget.CompanyId,
Model.CompanyList,
"--Company--",
new { #class = "form-control" })
</div>
<br />
<div class="dropdown">
#Html.DropDownListFor(m => m.FBudget.CustomerId,
Model.CustomerList,
"--Customer--",
new { #class = "form-control" })
</div>
<br />
</div>
<div class="col">
<div class="dropdown">
#Html.DropDownListFor(m => m.FBudget.Customer.LicensingArea,
Model.LicensingAreaList,
"--Licensing Area--",
new { #class = "form-control" })
</div>
<br />
<div class="dropdown">
#Html.DropDownListFor(m => m.FBudget.Year,
Model.YearsList,
"--Year--",
new { #class = "form-control" })
</div>
<br />
</div>
<div class="col">
<div class="dropdown">
#Html.DropDownListFor(m => m.FBudget.ItemMaster.ProductGroupId,
Model.ProductGroupList,
"--Product Group--",
new { #class = "form-control" })
</div>
<br />
<div class="dropdown">
#Html.DropDownListFor(m => m.FBudget.ItemMaster.PharmaFormId,
Model.PharmaFormList,
"--Pharma Form--",
new { #class = "form-control" })
</div>
<br />
</div>
<div class="col">
<div class="dropdown">
#Html.DropDownListFor(modelItem => modelItem.FBudget.Currency,
Model.CurrencyList,
"--Currency--",
new { #class = "form-control" })
</div>
<br />
<div class="dropdown">
#Html.DropDownListFor(m => m.FBudget.LedgerTypeId,
Model.LedgerScenarioList,
"--Scenario--",
new { #class = "form-control" })
</div>
<br />
</div>
<div class="dropdown">
<button type="submit" class="btn btn-primary form-control">Go</button>
</div>
</div>
</form>
<hr />
<div class="row">
<div class="col-6">
<h2 class="text-primary">Budget List</h2>
</div>
<div class="col-6">
<a asp-action="Upsert" class="btn btn-primary float-end"><i class="fas fa-plus"></i> Create New Budget</a>
</div>
</div>
<br />
<br />
<table class="table">
<thead>
<tr>
<th>
Customer
</th>
<th>
Decription
</th>
<th>
Curr
</th>
<th>
FoC
</th>
<th>
UM
</th>
<th>
Price
</th>
<th>
Quantity
</th>
<th>
Total amount
</th>
<th>
Action
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.RecordsList)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Customer.CustomerName)
</td>
<td>
#Html.DisplayFor(modelItem => item.ItemMaster.ItemDescription)
</td>
<td>
#Html.DisplayFor(modelItem => item.Currency)
</td>
<td>
#Html.DisplayFor(modelItem => item.FreeOfCharge)
</td>
<td>
#Html.DisplayFor(modelItem => item.UnitOfMeasure)
</td>
<td>
#Html.DisplayFor(modelItem => item.UnitPrice)
</td>
<td>
#Html.DisplayFor(modelItem => item.Quantity)
</td>
<td>
#Html.DisplayFor(modelItem => item.TotalAmount)
</td>
<td>
<a asp-action="Upsert" asp-route-id="#item.BudgetId">Edit</a> |
<a asp-action="Delete" asp-route-id="#item.BudgetId">Delete</a>
</td>
</tr>
}
</tbody>
</table>
And here is the Index Post method in the Controller:
/********INDEX POST*********/
[HttpPost]
public IActionResult Index(BudgetViewModel budget)
{
BudgetVM = new BudgetViewModel()
{
FBudget = new FBudget(),
YearsList = _unitOfWork.Budget.GetYearsListForDropdown(),
CompanyList = _unitOfWork.Budget.GetCompanyListForDropDown(),
CustomerList = _unitOfWork.Budget.GetCustomerListForDropDown(),
ProductGroupList = _unitOfWork.Budget.GetProductGroupListForDropDown(),
LicensingAreaList = _unitOfWork.Budget.GetLicensingAreaListForDropDown(),
PharmaFormList = _unitOfWork.Budget.GetPharmaFormListForDropDown(),
LedgerScenarioList = _unitOfWork.Budget.GetLedgerScenarioListForDropDown(),
CurrencyList = _unitOfWork.Budget.GetCurrencyListForDropdown(),
//Here isd where I try to filter data
RecordsList = _unitOfWork.Budget.GetAll(
filter: b => b.Year == budget.FBudget.Year
&& b.CompanyId == budget.FBudget.CompanyId
&& b.CustomerId == budget.FBudget.CustomerId
&& b.Customer.LicensingArea == budget.FBudget.Customer.LicensingArea
&& b.ItemMaster.ProductGroupId == budget.FBudget.ItemMaster.ProductGroupId
&& b.ItemMaster.PharmaFormId == budget.FBudget.ItemMaster.PharmaFormId
&& b.Currency == budget.FBudget.Currency
&& b.LedgerTypeId == budget.FBudget.LedgerTypeId
, includeProperties: "ItemMaster,Customer")
};
//return View(budget);
return View(BudgetVM);
}
Naturally with the logical operator && all fields must be inserted, otherwise the query result will be empty. How can I make these parameters optional, so that if I select only one filter (dropdown) the query is made only on that field, excluding the others?
Here is the GetAll() method:
public IEnumerable<T> GetAll(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = null)
{
IQueryable<T> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
//include properties will be comma seperated
if (includeProperties != null)
{
foreach (var includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
return query.ToList();
}
Thanks
You can either use something like PredicateBuilder to build the filter, or build it yourself using the System.Linq.Expressions namespace.
For example, assuming any filter properties which are not set will be null:
public static Expression<Func<Budget, bool>> BuildFilter(BudgetViewModel budget)
{
var b = Expression.Parameter(typeof(Budget), "b");
var filterParts = new List<Expression>();
if (budget.FBudget.Year is {} year)
{
var bugetYear = Expression.Property(b, nameof(Budget.Year);
var testYear = Expression.Constant(year);
filterParts.Add(Expression.Equal(budgetYear, testYear));
}
if (budget.FBudget.CompanyId is {} companyId)
{
var budgetCompanyId = Expression.Property(b, nameof(Budget.CompanyId));
var testCompanyId = Expression.Constant(companyId);
filterParts.Add(Expression.Equal(budgetCompanyId, testCompanyId));
}
if (budget.FBudget.CustomerId is {} customerId)
{
var budgetCustomerId = Expression.Property(b, nameof(Budget.CustomerId));
var testCustomerId = Expression.Constant(customerId);
filterParts.Add(Expression.Equal(budgetCustomerId, testCustomerId));
}
if (budget.FBudget.Customer.LicensingArea is {} licensingArea)
{
var budgetCustomer = Expression.Property(b, nameof(Budget.Customer));
var budgetLicensingArea = Expression.Property(budgetCustomer, nameof(Customer.LicensingArea));
var testLicensingArea = Expression.Constant(licensingArea);
filterParts.Add(Expression.Equal(budgetLicensingArea, testLicensingArea));
}
if (budget.FBudget.ItemMaster.ProductGroupId is {} productGroupId)
{
var budgetItemMaster = Expression.Property(b, nameof(Budget.ItemMaster));
var budgetProductGroupId = Expression.Property(budgetItemMaster, nameof(ItemMaster.ProductGroupId));
var testProductGroupId = Expression.Constant(productGroupId);
filterParts.Add(Expression.Equal(budgetProductGroupId, testProductGroupId));
}
if (budget.FBudget.ItemMaster.PharmaFormId is {} pharmaFormId)
{
var budgetItemMaster = Expression.Property(b, nameof(Budget.ItemMaster));
var budgetPharmaFormId = Expression.Property(budgetItemMaster, nameof(ItemMaster.PharmaFormId));
var testPharmaFormId = Expression.Constant(pharmaFormId);
filterParts.Add(Expression.Equal(budgetPharmaFormId, testPharmaFormId));
}
if (budget.FBudget.Currency is {} currency)
{
var budgetCurrency = Expression.Property(b, nameof(Budget.Currency));
var testCurrency = Expression.Constant(currency);
filterParts.Add(Expression.Equal(budgetCurrency, testCurrency));
}
if (budget.FBudget.LedgerTypeId is {} ledgerTypeId)
{
var budgetLedgerTypeId = Expression.Property(b, nameof(Budget.LedgerTypeId));
var testLedgerTypeId = Expression.Constant(ledgerTypeId);
filterParts.Add(Expression.Equal(budgetLedgerTypeId, testLedgerTypeId));
}
if (filterParts.Count == 0) return null;
var body = filterParts.Aggregate(Expression.AndAlso);
return Expression.Lambda<Func<Budget, bool>>(body, b);
}

VIEW MODAL UPDATE IN ASP.NET MVC and C#

I have a view, which shows a list of items consumed in a div and other details of a complaint in another div. Now, these items consumed div can have multiple records against a complaint. How to add new records and then refresh the div while still showing the original view. How to work around the problem. I did try to use modal popup but after submission and record update in the table, it's still showing the modal, whereas it needs to refresh the view itself.
This join view has edit option to set status, which leads to the problem view mentioned above.
In the status set view , i have multiple data such as id etc. in one div, Another div which contains the list of materials consumed so far. This div where consumption details are loaded as a list needs to be updated , as in new records to be updated and the updated view should be generated.
I need to dynamically add new material in the other div based on dropdown selection(hide/Show). and after submission, the tables needs to be updated.
public ActionResult StatusEdit(int id, int statuscode, string statusaction, int regid, short appid, short dep, short com )
{
DisplayEC dn = new DisplayEC();
DbAccess da = new DbAccess();
dn.ComplnId = com;
dn.JobC = id;
///dn.MatId = mat;
///dn.StDate = sdate;
dn.DeptCode = dep;
dn.Appid = appid;
dn.statcode = statuscode;
dn.statacn = statusaction;
dn.Regid = regid;
dn.JbStsDate = DateTime.Now.ToString();
dn.DeptCode = Convert.ToByte(Session["departmentno"]);
dn.ListJobStatus = new List<System.Web.Mvc.SelectListItem>();
dn.ListJobStatus = da.GetAllJobStatus(dn.DeptCode, appid);
dn.ListJobHistory = new List<JobStatus>();
dn.ListJobMaterials = new List<DispJobMaterial>();
dn.JobCode = dn.JobC.ToString();
if ((dn.DeptCode > 0) && (!string.IsNullOrEmpty(dn.JobCode)))
{
dn.ListJobHistory = da.GetJobStatusHistory(dn.DeptCode, dn.Appid, Convert.ToInt32(dn.JobC));
dn.ListJobMaterials = da.GetMaterialsConsumed(dn.DeptCode, Convert.ToByte(dn.Appid), Convert.ToInt32(dn.JobC));
}
dn.ListMaterials = new List<System.Web.Mvc.SelectListItem>();
dn.ListMaterials = da.GetMaterialsByUserType(dn.DeptCode, dn.Appid, "DEPTUSER");
return View(dn);
}
View:
#using System.Web.Helpers
#model EC.ViewModels.DisplayEC
#{
Layout = "~/Views/Shared/_Layout1.cshtml";
}
<br />
<br />
<a style="cursor: pointer; font-size: 30px; color:darkblue" title="Home" class="fa fa-home pull-left" href="#Url.Action("JoinView", "Admin")"></a>
<a style="cursor: pointer; font-size: 30px; color:darkblue" title="Log Out" class="fa fa-sign-out pull-right" href="#Url.Action("Login", "Account")"></a>
<br /><br />
<h4 class="display-4" style="display: inline;">JOBCODE :: <span style="color:red">#Model.JobCode</span></h4>
<div class="mt-5">
#*#using (Html.BeginForm("StatusEdit", "Admin", FormMethod.Post, new { id = "StatusEdit", #class = "", role = "form" }))*#
#using (Html.BeginForm("StatusEdit", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
#Html.HiddenFor(x => x.JobC)
#Html.HiddenFor(x => x.JobCode)
#Html.HiddenFor(x => x.DeptCode)
#Html.HiddenFor(x => x.Appid)
#Html.HiddenFor(x => x.statcode)
#Html.HiddenFor(x => x.statacn)
#Html.HiddenFor(x => x.Regid)
#Html.HiddenFor(x => x.JbStsDate)
#Html.HiddenFor(x => x.Regid)
#*#Html.HiddenFor(x => x.JobStatusCode)*#
#Html.HiddenFor(x => x.ComplnId)
#Html.HiddenFor(x => x.MatId)
#Html.HiddenFor(x => x.StDate)
<div>
#if (TempData["notice"] != null)
{
<h4 class="alert alert-danger" id="successMessage">#TempData["notice"]</h4>
}
</div>
<table class="table table-borderless" style="width:100%">
<tbody>
<tr>
<td style="width:50%">
<div class="card">
<div class="card-header bg-secondary"><span style="font-weight:bold;color:white">JOB STATUS</span></div>
<div class="card-body">
<table class="table table-borderless" style="width:100%;margin-bottom:0;padding-bottom:0">
<tbody>
<tr>
<td style="width:30%">
<div class="form-group">
#Html.LabelFor(x => x.JobStatusCode, new { #class = "control-label label_text" })
<div>
#if (string.IsNullOrEmpty(Model.JobCode))
{
#Html.DropDownListFor(x => x.JobStatusCode, new SelectList(Model.ListJobStatus.ToList().Where(x => x.Value != "1").Where(x => x.Value != "2").Where(x => x.Value != "3").Where(x => x.Value != "6").Where(x => x.Value != "7").Where(x => x.Value != "5").ToList(), "Value", "Text", Model.JobStatusCode), new { #class = "form-control", #disabled = "true", id = "ddlCategory" })
}
else
{
#Html.DropDownListFor(x => x.JobStatusCode, new SelectList(Model.ListJobStatus.ToList().Where(x => x.Value != "1").Where(x => x.Value != "2").Where(x => x.Value != "3").Where(x => x.Value != "6").Where(x => x.Value != "7").Where(x => x.Value != "5").ToList(), "Value", "Text", Model.JobStatusCode), new { #class = "form-control", id = "ddlCategory" })
}
#Html.ValidationMessageFor(x => x.JobStatusCode)
</div>
</div>
</td>
<td style="width:20%">
<div class="form-group">
#Html.LabelFor(x => x.JbStsDate, new { #class = "control-label label_text" })
<div>
#Html.TextBoxFor(x => x.JbStsDate, new { #class = "form-control" })
#Html.ValidationMessageFor(x => x.JbStsDate)
</div>
</div>
</td>
<td style="width:30%">
<div class="form-group">
#Html.LabelFor(x => x.JobStatusRemark, new { #class = "control-label label_text" })
<div>
#Html.TextBoxFor(x => x.JobStatusRemark, new { #class = "form-control" })
#Html.ValidationMessageFor(x => x.JobStatusRemark)
</div>
</div>
</td>
<td style="width:20%">
<div class="form-group" style="padding-top: 30px;">
<button id="btnAddSts" type="button" disabled="disabled" class="btn btn-info pull-left"><i class="fa fa-plus-square"></i> Add Status</button>
</div>
</td>
</tr>
</tbody>
</table>
<table id="JbStsHtry" class="table table-responsive-md table-hover" style="width:100%">
<tbody>
#for (int i = 0; i < Model.ListJobHistory.Count; i++)
{
<tr class="h-divider_sts">
<td style="width:40%">
<span>#Html.DisplayFor(x => x.ListJobHistory[i].StatusText)</span>
</td>
<td style="width:20%">
<span>#Model.ListJobHistory[i].StatusDate.ToShortDateString()</span>
</td>
<td style="width:30%">
<span>#Html.DisplayFor(x => x.ListJobHistory[i].StatusRemarks)</span>
</td>
<td style="width:10%">
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</td>
<td style="width:50%" id="Other">
<div class="card">
<div class="card-header bg-secondary">
<span style="font-weight:bold;color:white">Material Consumed</span>
</div>
<div class="card-body">
<table class="table table-borderless table-responsive-md" style="width:100%;margin-bottom:0;padding-bottom:0">
<tbody>
<tr style="width:20%">
<th>#Html.LabelFor(x => x.JobMaterialId, new { #class = "control-label label_text" })</th>
<th> #Html.LabelFor(x => x.MatrlQty, new { #class = "control-label label_text" })</th>
</tr>
<tr>
<td style="width:40%">
<div class="form-group">
#*#Html.LabelFor(x => x.JobMaterialId, new { #class = "control-label label_text" })*#
<div>
#if (string.IsNullOrEmpty(Model.JobCode))
{
#Html.DropDownListFor(x => x.JobMaterialId, new SelectList(Model.ListMaterials, "Value", "Text", Model.JobMaterialId), new { #class = "form-control", #disabled = "true" })
}
else
{
#Html.DropDownListFor(x => x.JobMaterialId, new SelectList(Model.ListMaterials, "Value", "Text", Model.JobMaterialId), new { #class = "form-control" })
}
#Html.ValidationMessageFor(x => x.JobMaterialId)
</div>
</div>
</td>
<td style="width:30%">
<div class="form-group">
#*#Html.LabelFor(x => x.MatrlQty, new { #class = "control-label label_text" })*#
<div>
#Html.TextBoxFor(x => x.MatrlQty, new { #class = "form-control" })
#Html.ValidationMessageFor(x => x.MatrlQty)
</div>
</div>
</td>
<td style="width:30%">
<div class="form-group" style="padding-top: 30px;">
<button id="btnAdd" class="btn btn-info pull-left"><i class="fa fa-plus-square"></i> Add Material</button>
</div>
</td>
</tr>
</tbody>
</table>
#*<table id="JbMatrl" class="table table-responsive-md table-hover" style="width:100%">*#
<table id="JbMatrl" class="table table-responsive-md table-hover" style="width:100%">
<tbody>
#for (int i = 0; i < Model.ListJobMaterials.Count; i++)
{
<tr class="h-divider_sts">
<td style="width:50%">
#Html.DisplayFor(x => x.ListJobMaterials[i].MaterialText)
</td>
<td style="width:50%">
#Html.DisplayFor(x => x.ListJobMaterials[i].MaterialQty)
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</td>
<td style="width:50%" id="Other2">
</td>
</tr>
</tbody>
</table>
<div class="form-group" style="padding-top: 30px;">
<button type="submit" class="btn btn-info pull-left"><i class="fa fa-plus-square"></i> Submit</button>
</div>
}
</div>
<br />
\\\\\\\\
<script type="text/javascript">
$(document).ready(function () {
$("#ddlCategory").change(function () {
var e = document.getElementById("ddlCategory");
var value = e.options[e.selectedIndex].text;
if (value == "Assignment Cancelled") {
$('#Other').hide();
$('#Other2').show();
} else {
$('#Other').show();
$('#Other2').hide();
}
});
});
</script>

I want to add paging on a multiple view but i get error

I got multiple views but I am getting an error when I use the NuGet.
Here is my controller Code:
public ActionResult Index(string searchString, int? id, int? courseID, int? idcom, int? elencoimo, int? elecoimobi,int? elencoinsta)
{
var viewmodel = new mainview();
viewmodel.elencoomonimi = db.ElencoOmonimis
.Where(s => s.Nome.Contains(searchString) || searchString == null || searchString == "")
.Include(s => s.ElencoProvinces.Select(t => t.ElencoImmobiliPerDiritti_E_Quote))
.Include(s => s.ElencoProvinces.Select(t => t.ElencoComunis))
.Include(s => s.ElencoProvinces.Select(t => t.ElencoImmobiliPerDiritti_E_Quote.Select(r => r.ElencoIntestatis)))
//.Include(i => i.ElencoOmonimi)
//.Include(i => i.ElencoImmobiliPerDiritti_E_Quote.Select(t => t.ElencoIntestatis))
//.Include(i => i.ElencoComunis)
.OrderBy(i => i.Id);
if (id != null)
{
ViewBag.elencoomonimiID = id.Value;
viewmodel.elencoprovince = viewmodel.elencoomonimi.Where(
i => i.Id == id.Value).Single().ElencoProvinces;
}
if (idcom != null)
{
ViewBag.ElencoImmobiliperID = idcom.Value;
viewmodel.elencointestati = viewmodel.elencoimmobiliperditti.Where(
x => x.Id == idcom.Value).Single().ElencoIntestatis;
}
if (elencoimo != null)
{
ViewBag.elecomuniID = elencoimo.Value;
viewmodel.elencocomuni = viewmodel.elencoprovince.Where(
r => r.Id == elencoimo.Value).Single().ElencoComunis;
}
if(elecoimobi != null)
{
ViewBag.ElencoimobID = elecoimobi.Value;
viewmodel.elencoimmobiliperditti = viewmodel.elencoprovince.Where(
q => q.Id == elecoimobi.Value).Single().ElencoImmobiliPerDiritti_E_Quote;
}
if (elencoinsta != null)
{
ViewBag.ElencoInstatiID = elencoinsta.Value;
viewmodel.elencointestati= viewmodel.elencoimmobiliperditti.Where(
e => e.Id == elencoinsta.Value).Single().ElencoIntestatis;
}
//if(elencoimo != null)
// ViewBag.elencoimoobiliID = id.Value
//viewmodel.elencoimmobiliperditti = db.ElencoImmobiliPerDiritti_E_Quote
// .Include(r => r.ElencoIntestatis)
// .OrderBy(r => r.Classe);
return View(viewmodel);
}
and here is my view:
#model automasis.Models.mainview
#{
ViewBag.Title = "Index";
}
Dashboard
#using (Html.BeginForm())
{
#Html.TextBox("SearchString") <br />
<input type="submit" value="Filter" />
}
<table class="table">
<tr>
<th>
Nome
</th>
<th>
Cognome
</th>
<th>
Nome Cercato
</th>
<th>
Cognome Cercato
</th>
<th>
Data Di Nascita
</th>
<th>
Data Di Sesso
</th>
<th></th>
</tr>
#foreach (var item in Model.elencoomonimi)
{
string selectedRow = "";
if (item.Id == ViewBag.elencoomonimiID)
{
selectedRow = "success";
}
<tr class="#selectedRow">
<td>
#Html.DisplayFor(modelItem => item.Nome)
</td>
<td>
#Html.DisplayFor(modelItem => item.Cognome)
</td>
<td>
#Html.DisplayFor(modelItem => item.NomeCercato)
</td>
<td>
#Html.DisplayFor(modelItem => item.CognomeCercato)
</td>
<td>
#Html.DisplayFor(modelItem => item.DataDiNascita)
</td>
<td>
#Html.DisplayFor(modelItem => item.Sesso)
</td>
#*#if (item.ElencoProvinces != null)
{
#item.ElencoProvinces
}*#
<td>
#Html.ActionLink("Select", "Index", new { id = item.Id }) |
#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>
#if (Model.elencoprovince != null)
{
<h3>Provinca</h3>
<table class="table">
<tr>
<th></th>
<th>Provincia</th>
<th>Terreni</th>
<th>Fabricati</th>
</tr>
#foreach (var item in Model.elencoprovince)
{
string selectedRow = "";
if (item.Id == ViewBag.elencoprovinceID)
{
selectedRow = "success";
}
<tr class="#selectedRow">
<td>
#Html.ActionLink("Immobiliper", "Index", new { elecoimobi = item.Id })
</td>
<td>
#Html.ActionLink("Comune", "Index", new { elencoimo = item.Id })
</td>
<td>
#item.Provincia
</td>
<td>
#item.Terreni
</td>
<td>
#item.Fabbricati
</td>
<td class="hidden">
#if (item.ElencoImmobiliPerDiritti_E_Quote != null)
{
#item.ElencoImmobiliPerDiritti_E_Quote
}
</td>
</tr>
}
</table>
}
#if (Model.elencocomuni != null)
{
<h3>Elenco Comuni</h3>
<table class="table">
<tr>
<th></th>
<th>Particella</th>
<th>Sub</th>
<th>Titolarita</th>
<th>Ubicazione</th>
<th>Partita</th>
</tr>
#foreach (var item in Model.elencocomuni)
{
string selectedRow = "";
if (item.Id == ViewBag.elecomuniID)
{
selectedRow = "success";
}
<tr class="#selectedRow">
<td>
#Html.ActionLink("Select", "Index", new { id = item.Id })
</td>
<td>
#item.Comune
</td>
<td>
#item.Fabbricati
</td>
<td>
#item.Terreni
</td>
</tr>
}
</table>
}
#if (Model.elencointestati != null)
{
<h3>elencoimmobiliperditti</h3>
<table class="table">
<tr>
<th></th>
<th>Particella</th>
<th>Sub</th>
<th>Titolarita</th>
<th>Ubicazione</th>
<th>Partita</th>
</tr>
#foreach (var item in Model.elencointestati)
{
string selectedRow = "";
if (item.Id == ViewBag.ElencoInstatiID)
{
selectedRow = "success";
}
<tr class="#selectedRow">
<td>
#Html.ActionLink("Select", "Index", new { id = item.Id })
</td>
<td>
#item.Nominativo
</td>
<td>
#item.Titolarita
</td>
</tr>
}
</table>
}
#if (Model.elencoimmobiliperditti != null)
{
<h3>elencoimmobiliperditti</h3>
<table class="table">
<tr>
<th></th>
<th>Particella</th>
<th>Sub</th>
<th>Titolarita</th>
<th>Ubicazione</th>
<th>Partita</th>
</tr>
#foreach (var item in Model.elencoimmobiliperditti)
{
string selectedRow = "";
if (item.Id == ViewBag.ElencoInstatiID)
{
selectedRow = "success";
}
<tr class="#selectedRow">
<td>
#Html.ActionLink("Select", "Index", new { elencoinsta = item.Id })
</td>
<td>
#item.Classe
</td>
<td>
#item.Consistenza
</td>
<td>
#item.Foglio
</td>
<td class="hidden">
#if (item.Partita != null)
{
#item.Rendita
}
</td>
</tr>
}
</table>
}

Default routing don't work with pagination on mvc 5

Greetings I have developed an ASP.NET MVC 5 application. I'm using pagination, it works but when I want to call a query that I wrote I get the error:
Additional information: Object reference not set to an instance of an object.
I think its a problem on routing but I can't figure it out.
Here is my controller:
public ActionResult Index(string searchString, int? omonimipage, int? id, int? courseID, int? idcom, int? elencoimo, int? elecoimobi, int? elencoinsta, int? page)
{
int elencoomoninumber = (omonimipage ?? 1);
var viewmodel = new mainview();
viewmodel.elencoomonimi = db.ElencoOmonimis
.Include(s => s.ElencoProvinces)
.Include(s => s.ElencoProvinces.Select(r => r.ElencoImmobiliPerDiritti_E_Quote))
.Include(s => s.ElencoProvinces.Select(r => r.ElencoComunis))
.Include(s => s.ElencoProvinces.Select(r => r.ElencoImmobiliPerDiritti_E_Quote.Select(q => q.ElencoIntestatis)))
.OrderBy(i => i.Id).ToPagedList(elencoomoninumber,6);
if (id != null)
{
ViewBag.elencoprovinceID = id.Value;
viewmodel.elencoprovince = viewmodel.elencoomonimi.Where(
i => i.Id == id.Value).SingleOrDefault().ElencoProvinces;
}
if (idcom != null)
{
ViewBag.ElencoImmobiliperID = idcom.Value;
viewmodel.elencointestati = viewmodel.elencoimobili.Where(
r => r.Id == idcom.Value).Single().ElencoIntestatis;
}
if (elencoimo != null)
{
ViewBag.elecomuniID = elencoimo.Value;
viewmodel.elencocomuni = viewmodel.elencoprovince.Where(
r => r.Id == elencoimo.Value).Single().ElencoComunis;
}
if (elecoimobi != null)
{
ViewBag.ElencoimobID = elecoimobi.Value;
viewmodel.elencoimobili = viewmodel.elencoprovince.Where(
q => q.Id == elecoimobi.Value).Single().ElencoImmobiliPerDiritti_E_Quote;
}
if (elencoinsta != null)
{
ViewBag.ElencoInstatiID = elencoinsta.Value;
viewmodel.elencointestati = viewmodel.elencointestati = db.ElencoImmobiliPerDiritti_E_Quote.Where(
e => e.Id == elencoinsta.Value).Single(i =>i.Id == elencoinsta.Value).ElencoIntestatis;
}
return View(viewmodel);
}
and here is my view:
#model automa0._1.Models.mainview
#using PagedList.Mvc;
#{
ViewBag.Title = "Index";
}
Dashboard
#using (Html.BeginForm())
{
#Html.TextBox("SearchString") <br />
<input type="submit" value="Filter" />
}
<div id="content">
<table class="table">
<tr>
<th>
Nome
</th>
<th>
Cognome
</th>
<th>
Data Di Nascita
</th>
<th>
Codice Fiscale
</th>
<th></th>
</tr>
#foreach (var item in Model.elencoomonimi)
{
string selectedRow = "";
if (item.Id == ViewBag.elencoomonimiID)
{
selectedRow = "success";
}
<tr class="#selectedRow">
<td>
#Html.DisplayFor(modelItem => item.CognomeCercato)
</td>
<td>
#Html.DisplayFor(modelItem => item.NomeCercato)
</td>
<td>
#Html.DisplayFor(modelItem => item.Cognome)
</td>
<td>
#Html.DisplayFor(modelItem => item.DataDiNascita)
</td>
<td>
#Html.DisplayFor(modelItem => item.CodiceFiscale)
</td>
<td>
#Html.ActionLink("Provinca", "Index", new { id = item.Id }) |
</td>
</tr>
}
</div>
</>
<div id="contentPager">
Page #(Model.elencoomonimi.PageCount < Model.elencoomonimi.PageNumber ? 0 : Model.elencoomonimi.PageNumber) of #Model.elencoomonimi.PageCount
#Html.PagedListPager(Model.elencoomonimi, page => Url.Action("Index", new { omonimipage = page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
</div>
</table>
#if (Model.elencoprovince != null)
{
<h3>Provinca</h3>
<table class="table">
<tr>
<th></th>
<th>Provincia</th>
<th>Terreni</th>
<th>Fabbricati</th>
</tr>
#foreach (var item in Model.elencoprovince)
{
string selectedRow = "";
if (item.Id == ViewBag.elencoprovinceID)
{
selectedRow = "success";
}
<tr class="#selectedRow">
<td>
#Html.ActionLink("Select", "Index", new { elecoimobi = item.Id })
</td>
<td>
#item.Provincia
</td>
<td>
#item.Terreni
</td>
<td>
#item.Fabbricati
</td>
#if (item.ElencoImmobiliPerDiritti_E_Quote != null)
{
#item.ElencoImmobiliPerDiritti_E_Quote
}
</tr>
}
</table>
}
#if (Model.elencoimobili != null)
{
<h3>elencoimmobiliperditti</h3>
<table class="table">
<tr>
<th></th>
<th>Classe</th>
<th>Consistenza</th>
<th>Foglio</th>
</tr>
#foreach (var item in Model.elencoimobili)
{
string selectedRow = "";
if (item.Id == ViewBag.ElencoImmobiliperID)
{
selectedRow = "success";
}
<tr class="#selectedRow">
<td>
#Html.ActionLink("Select", "Index", new { elencoinsta = item.Id })
</td>
<td>
#item.Classe
</td>
<td>
#item.Consistenza
</td>
<td>
#item.Foglio
</td>
</tr>
}
</table>
}
#if (Model.elencointestati != null)
{
<h3>elneco Intestati</h3>
<table class="table">
<tr>
<th>CodiceFiscale</th>
<th>Nominativo</th>
<th>Titolarita</th>
</tr>
#foreach (var item in Model.elencointestati)
{
string selectedRow = "";
if (item.Id == ViewBag.ElencoImmobiliperID)
{
selectedRow = "success";
}
<tr class="#selectedRow">
<td>
#Html.ActionLink("Select", "Index", new { id = item.Id })
</td>
<td>
#item.CodiceFiscale
</td>
<td>
#item.Nominativo
</td>
<td>
#item.Titolarita
</td>
</tr>
}
</table>
}
on default its working but wen i navigate to page 2 the url its: http://localhost:49208/ElencoOmonimis?omonimipage=2
Thank you.

Read files from database and put it into my Combo box over HTML page

I try to add my files into DropDownList, all my files with the relevant property already return by the controller:
// GET: /WebMail/
public ActionResult Index()
{
var fileNameList = db.Captures.ToList().Where(x => x.robotFamily == "WebMail");
//return View(db.Captures.ToList().Where(x => x.robotFamily == "WebMail"));
ViewBag.Files = fileNameList;
return View();
}
Index.cshtml
#model IEnumerable<AutomationCapturesMVC.Models.Capture>
#{
//ViewBag.Title = "Index";
}
<table class="table">
<tr>
<th style="font-size: 20px">
#Html.DisplayNameFor(model => model.fileName)
</th>
<th></th>
</tr>
#Html.DropDownList("Id", (IEnumerable<AutomationCapturesMVC.Models.Capture>)ViewBag.Files, new { id = "WebMail", #class = "btn dropdown-toggle" })
#foreach (var item in Model)
{
<tr>
<td style="font-size: 15px">
#Html.DisplayFor(modelItem => item.fileName)
</td>
<td>
#Html.ActionLink("File Details", "Details", new { id = item.id })
</td>
</tr>
}
</table>
this is my page error:
http://s21.postimg.org/983eokpfq/1231231313.jpg
How about the following?
In controller
public ActionResult Index()
{
var fileNames = GetDatabaseFileNames();
var fileNameList = fileNames.Select(d => new SelectListItem { Text = d.FileName, Value = d.FileName.ToString() }).ToList();
ViewBag.Files = fileNameList;
return View();
}
In View
#Html.DropDownList("DatabaseFiles", (IEnumerable<SelectListItem>)ViewBag.Files, new { id = "DatabaseFiles", #class = "btn dropdown-toggle" })

Categories