Why are the parameters of my action null? - c#

I'm trying to make a page using ASP MVC 5 with C# in which I have some controls to type search parameters and a table where the results are shown.
The thing is that the user should be allowed to edit the text of two fields in the table and save the changes.
To accomplish this, my view receives a model with two objects as properties, one for the search parameters and one for the result list, like this:
public class SEARCH_PAGE
{
public List<VIEW_DATA_APR> table{ get; set; }
public SEARCH parameters{ get; set; }
}
Then my Controller is this:
public class CargaAPRController : Controller
{
private datasource db = new datasource();
// GET: CargaAPR
public ActionResult Index()
{
try
{
List<SelectListItem> items = new SelectList(db.COMPANY, "IDCOMPANY", "COMPANYNAME").ToList();
items.Insert(0, (new SelectListItem { Text = "ALL", Value = "0" }));
ViewData["IDCOMPANY"] = items;
var letters = (from c in db.LETTERS
select new VIEW_DATA_APR
{
company = c.COMPANY.COMPANYNAME,
idLetter = c.IDLETTER,
nic = "not found",
client = "not found",
energy = 0,
money = 0,
period = "",
letterCode = c.LETTERCODE
});
SEARCH_PAGE sp = new SEARCH_PAGE();
sp.table= letters.ToList();
sp.parameters= new SEARCH();
return View(sp);
}
catch (Exception ex)
{
return RedirectToAction("Error", new RouteValueDictionary(new { controller = "Users", action = "Error", mensaje = "Error: " + ex.Message }));
}
}
[HttpPost]
public ActionResult Index(SEARCH_PAGE model)
{
try
{
List<SelectListItem> items = new SelectList(db.COMPANY, "IDCOMPANY", "COMPANYNAME").ToList();
items.Insert(0, (new SelectListItem { Text = "ALL", Value = "0" }));
ViewData["IDCOMPANY"] = items;
decimal company = Decimal.Parse(model.parameters.company);
var letters= (from c in db.LETTERS
where (company== 0 ? c.IDCOMPANY: company) == c.IDCOMPANY
select new VIEW_DATA_APR
{
company= c.COMPANY.COMPANYNAME,
idLetter= c.IDLETTER,
nic = "not found",
client = "not found",
energy = 0,
money = 0,
period = "",
letterCode = c.LETTERCODE
});
SEARCH_PAGE sp = new SEARCH_PAGE();
sp.table= letters.ToList();
sp.parameters = model.parameters;
return View(sp);
}
catch (Exception ex)
{
return RedirectToAction("Error", new RouteValueDictionary(new { controller = "Users", action = "Error", mensaje = "Error: " + ex.Message }));
}
}
[HttpPost]
public ActionResult Save(SEARCH_PAGE model_search_page )
{
return View();
}
}
And my View is:
#using datasource.Models
#model SEARCH_PAGE
#{
ViewBag.Title = "Load APR";
Layout = "~/Views/Shared/_Layout.cshtml";
}
#Styles.Render("~/Content/energetica.css")
<meta name="viewport" content="width=device-width" />
<title>Letters</title>
<link href="#Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<div class="container-fluid">
<div class="col-md-1">
</div>
<div class="col-md-10">
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">
<div class="row">
<div class="col-md-1">
<a href="#Url.Action("Index", "LoadFiles")" class="elements">
<img class="img img-responsive" style="width:25px; height:26px;padding:1px" src="~/img/BackIcon.png">
</a>
</div>
<div class="col-md-11 text-left" style="padding:1px;">
LOAD APR
</div>
</div>
</div>
</div>
#using (Html.BeginForm("Index","LoadAPR", FormMethod.Post, null))
{
<table style="width:100%">
<tr>
<td style="width:10%">
<b>COMPANY: </b>
</td>
<td style="width:20%">
#Html.DropDownListFor(m => m.parameters.company, (IEnumerable<SelectListItem>)ViewData["IDCOMPANY"], new { htmlAttributes = new { #class = "form-control" } })
</td>
<td style="width:10%">
<b>PERIOD: </b>
</td>
<td style="width:20%">
#Html.EditorFor(m => m.parameters.period)
</td>
<td style="width:20%; text-align:right">
<input type="submit" name="SEARCH" value="SEARCH" />
</td>
</tr>
<tr>
<td style="width:10%">
<b>CLIENT: </b>
</td>
<td style="width:20%">
#Html.EditorFor(m => m.parameters.client)
</td>
<td style="width:10%">
<b>NIC: </b>
</td>
<td style="width:20%">
#Html.EditorFor(m => m.parameters.nic)
</td>
</tr>
</table>
}
<br />
#using (Html.BeginForm("Save", "LoadAPR", FormMethod.Post, null))
{
<div style="overflow-y: scroll; max-height: 300px">
<table style="width:100%">
<tr>
<th>
#Html.Label("LBDIS", "Company")
</th>
<th>
#Html.Label("LBNLETTER", "Letter Code")
</th>
<th>
#Html.Label("LBNIC", "NIC")
</th>
<th>
#Html.Label("LBCLIENT", "Client")
</th>
<th>
#Html.Label("LBENERGY", "Energy")
</th>
<th>
#Html.Label("LBMONEY", "Money")
</th>
</tr>
#foreach (var item in Model.table)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.company)
</td>
<td>
#Html.DisplayFor(modelItem => item.letterCode)
</td>
<td>
#Html.DisplayFor(modelItem => item.nic)
</td>
<td>
#Html.DisplayFor(modelItem => item.client)
</td>
<td>
#Html.EditorFor(modelItem => item.energy)
</td>
<td>
#Html.EditorFor(modelItem => item.money)
</td>
</tr>
}
</table>
</div>
<br />
<div style="width:100%; text-align:right;">
<input class="btn-addUser" type="submit" value="Save" />
</div>
}
</div>
So when I run it and click on my first button, the POST Index parameter receives just the parameters part of the object, the table part is null
And when I click on my second button, the POST Save parameter comes null in both properties.
I have tried so many different ways of seding the Model as parameter but nothing is working. Could you tell me what's wrong? Am I using he BeginForm in the correct way?
Thank you in advance.

Related

How do I pass a selected ID to my SQL Database?

I am having trouble with this last part of my project. I have all of my SQL Table columns filled up except for this last part. I have two tables WorkSchedule and WorkShiftBid.
In WorkShiftBid, I have WorkShiftBidID,WSBidDateTime,WSBidStatus,WorkScheduleID,StaffID
I will be using Guid.NewGuid() for WorkShiftBidID, Datetime.now for WSBidDatetime, A hardcoded "pending" value for WSBidStatus, A viewbag for StaffID and I am trying to get the WorkScheduleID to work but to no avail.
For WorkSchedule, I am trying to get just the "WorkScheduleID" attribute just by using a button method asp-route-id="#item.WorkScheduleID" and it will redirect me to the Create page whereby I will select a Staff from the drop-down list and then save it to my WorkShiftBid table as it has a foreign key for "WorkScheduleID".
It would be helpful if someone could help me. Thank you.
Controller Code:
public IActionResult CreateWorkShift()
{
ViewData["StaffID"] = new SelectList(_context.Staff, "StaffID", "StaffName");
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> CreateWorkShift(Guid? id, [Bind("WorkShiftBidID,WSBidDateTime,WSBidStatus,WorkScheduleID,StaffID")] WorkShiftBidModel workShiftBidModel)
{
if (id != workShiftBidModel.WorkScheduleID)
{
return NotFound();
}
if (ModelState.IsValid)
{
workShiftBidModel.WSBidDateTime = DateTime.Now;
workShiftBidModel.WorkShiftBidID = Guid.NewGuid();
_context.Add(workShiftBidModel);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(DisplaySchedule));
}
ViewData["StaffID"] = new SelectList(_context.Staff, "StaffID", "StaffName", workShiftBidModel.StaffID);
return View(workShiftBidModel);
}
private bool WorkShiftBidModelExist(Guid id)
{
return _context.WorkSchedule.Any(e => e.WorkScheduleID == id);
}
Display WorkShift Page:
<h1 class="text-center"> Apply Workshifts</h1>
<hr />
<table class="table">
<thead>
<tr>
<th>
From (DateTime)
</th>
<th>
To (DateTime)
</th>
<th>
Day
</th>
<th>
Status
</th>
<th>
Work Descriptions
</th>
<th>
Branch
</th>
<th>
Manager
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.WorkScheduleFromDateTime)
</td>
<td>
#Html.DisplayFor(modelItem => item.WorkScheduleToDateTime)
</td>
<td>
#Html.DisplayFor(modelItem => item.WorkScheduleFromDateTime.DayOfWeek)
</td>
<td>
#Html.DisplayFor(modelItem => item.WorkScheduleStatus)
</td>
<td>
#Html.DisplayFor(modelItem => item.WorkDescriptions.WorkDescriptionName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Branches.BranchName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Staff.StaffName)
</td>
<td>
<form method="post">
<input type="submit" class="btn btn-primary btn-block" value="Bid" asp-route-id="#item.WorkScheduleID" asp-action="DisplaySchedule" asp-controller="PartTimer" />
</form>
<div class="form-group col-md-6">
<a asp-action="CreateWorkShift" asp-route-id="#item.WorkScheduleID" class="btn btn-success">Create</a>
</div>
</td>
</tr>
}
</tbody>
</table>
Create view page code:
<h1>Create</h1>
<h4>Work Shifts</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="CreateWorkShift">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="WorkScheduleID"/>
<div class="form-group">
<label asp-for="WSBidStatus" class="control-label"></label>
<input readonly asp-for="WSBidStatus" value="Pending" class="form-control"/>
<span asp-validation-for="WSBidStatus" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="StaffID" class="control-label"></label>
<select asp-for="StaffID" class="form-control" asp-items="ViewBag.StaffID"></select>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<input type="submit" value="Save" class="btn btn-primary btn-block" />
</div>
<div class="form-group col-md-6">
<a asp-action="ProfilePage" class="btn btn-secondary btn-block"><i class=" fa fa-table"></i>Back to List</a>
</div>
</div>
</form>
</div>
</div>
#section Scripts {
#{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Do you mean you cannot get the WorkScheduleID in public async Task<IActionResult> CreateWorkShift(Guid? id, [Bind("WorkShiftBidID,WSBidDateTime,WSBidStatus,WorkScheduleID,StaffID")] WorkShiftBidModel workShiftBidModel).If so,you need to get and pass Id in public IActionResult CreateWorkShift.Here is a demo worked:
public IActionResult CreateWorkShift(int id)
{
List<Staff> list = new List<Staff> { new Staff { StaffID = 1, StaffName = "staff1" }, new Staff { StaffID = 2, StaffName = "staff2" } };
ViewData["StaffID"] = new SelectList(list, "StaffID", "StaffName");
WorkShiftBidModel w = new WorkShiftBidModel { WorkScheduleID = id };
return View(w);
}
result:

could not update or edit the specific data in the HTTP post method

I wanted to update the quantity value. for that, I am using the HTTPPost method of the index for update quantity. but it's could not because of error.
here is my code:
public IActionResult Index()
{
return View(_db.Shop.Include(c => c.Category).Include(f => f.SubCategory).ToList());
}
[HttpPost]
public IActionResult Index(int? id)
{
var product = _db.Shop.Where(x => x.Id == id).FirstOrDefault();
IndexVm ji = new IndexVm
{
Id = product.Id,
Image = product.Image,
Image1 = product.Image1,
Quantity = product.Quantity++,
Price = product.Price,
PreviousPrice = product.PreviousPrice,
Description = product.Description,
};
_db.Shop.Update(ji);
_db.SaveChanges();
return RedirectToAction(nameof(Index));
}
Index.cshtml
#using Amazon.Models
#model IEnumerable<Shop>
#{
ViewData["Title"] = "Index";
}
<br /><br />
<div class="row">
<div class="col-6">
<h2 class="text-info">Product List</h2>
</div>
<div class="col-6 text-right">
<a asp-action="Create" class="btn btn-info"> Add New Product </a>
</div>
</div>
<form asp-action="Create" method="post" enctype="multipart/form-data">
<div>
<table class="table table-striped border" id="myTable">
<thead>
<tr class="table-info">
<th>
#Html.DisplayNameFor(c => c.Name)
</th>
<th>
#Html.DisplayNameFor(c => c.Price)
</th>
<th>
#Html.DisplayNameFor(c => c.PreviousPrice)
</th>
<th>
#Html.DisplayNameFor(c => c.Quantity)
</th>
<th>
#Html.DisplayNameFor(c => c.Description)
</th>
<th>
#Html.DisplayNameFor(c => c.Quantity)
</th>
<th>
#Html.DisplayNameFor(c => c.CategoryTypeId)
</th>
<th>
#Html.DisplayNameFor(c => c.SubCategoryTypeId)
</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr class="text-info">
<td> #item.Name </td>
<td> #item.Price </td>
<td> #item.PreviousPrice </td>
<td>#item.Quantity</td>
<td>
<partial name="_QuantityPartial" model="#item.Id" />
</td>
<td> #item.Description </td>
<td> #item.Size </td>
<td> #item.Category.CategoryName </td>
<td> #item.SubCategory.SubCategoryName </td>
<td>
<partial name="_DeletePartial" model="#item.Id" />
</td>
</tr>
}
</tbody>
</table>
</div>
</form>
<br /> <br />
#section scripts{
<script type="text/javascript">
$(document).ready(function () {
$('#myTable').DataTable();
});
</script>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script>
function incrementValue(e) {
e.preventDefault();
var fieldName = $(e.target).data('field');
var parent = $(e.target).closest('div');
var currentVal = parseInt(parent.find('input[name=' + fieldName + ']').val(), 10);
if (!isNaN(currentVal)) {
parent.find('input[name=' + fieldName + ']').val(currentVal + 1);
} else {
parent.find('input[name=' + fieldName + ']').val(0);
}
}
function decrementValue(e) {
e.preventDefault();
var fieldName = $(e.target).data('field');
var parent = $(e.target).closest('div');
var currentVal = parseInt(parent.find('input[name=' + fieldName + ']').val(), 10);
if (!isNaN(currentVal) && currentVal > 0) {
parent.find('input[name=' + fieldName + ']').val(currentVal - 1);
} else {
parent.find('input[name=' + fieldName + ']').val(0);
}
}
$('.input-group').on('click', '.button-plus', function (e) {
incrementValue(e);
});
$('.input-group').on('click', '.button-minus', function (e) {
decrementValue(e);
});
</script>
}
_QuantityPartial.cshtml
#model int
<form method="post">
<td style="width:150px">
<div class="btn-group">
#*<a asp-action="Index" class="btn btn-danger" asp-route-id="#Model">Add</a>*#
<input type="submit" asp-action="Index" asp-route-id="#Model" value="+" />
</div>
</td>
</form>
and here is my output and here is my output below. I want something like that. I actually want when I click "+" then quantity would be updated. and here is my output below. I want something like that. I actually want when I click "+" then quantity would be updated. for that here I use the HTTP post method(index) but I found an error. for that, I can not update my quantity data.
I am beginner, please anyone help.
You should use:
var product = _db.Shop.AsNoTracking().Where(x => x.Id == id).FirstOrDefault();
You are fetching product but updating new instance ji with the same id. Since the id is the same, EF thinks that those are two objects with same primary key which should not happen, thus error.
Your other option would be to update product that you fetched from the database:
[HttpPost]
public IActionResult Index(int? id)
{
var product = _db.Shop.Where(x => x.Id == id).FirstOrDefault();
product.Quantity++;
_db.Shop.Update(product);
_db.SaveChanges();
return RedirectToAction(nameof(Index));
}
You can find more about this here: https://learn.microsoft.com/en-us/ef/core/querying/tracking
You are doing these two mistakes:
You are passing VM to update function which is wrong.
To update any existing EF object, you have to update that object instead of creating new.
{
var product = _db.Shop.Where(x => x.Id == id).FirstOrDefault();
product.Quantity++;
_db.Shop.Update(product);
_db.SaveChanges();
return RedirectToAction(nameof(Index));
}

How to add a counter with row numbering on all site in mvc

I want to add an automatic numbering of order items to all pages.
Below is an example that works but only numbers on one page. After going to the next page, it starts from the beginning.
public ActionResult ListaZlecen(int? IdStatusZlecenia, int strona = 1)
{
var ListaZlecenWszystkich = db.Zlecenia.ToList();
var userId = User.Identity.GetUserId();
//var ListaZlecen = db.Zlecenia.Where(p => p.UserId == userId).ToList();
//var ListaZlecen = db.Zlecenia.OrderBy(w => w.IdZlecenia).Where(p => p.UserId == userId).Skip((strona - 1) * WielkoscStrony).Take(WielkoscStrony).ToList();
var ListaZlecen = db.Zlecenia.OrderBy(w => w.IdZlecenia).Skip((strona - 1) * WielkoscStrony).Take(WielkoscStrony).ToList();
// var zleceniezalacznik = db.ZleceniaZalaczniki.ToList();
var viewodel = new ListaZlecenUzytkownikaViewModel()
{
StronaInfo = new StronaInfo
{
AktualnaStrona = strona,
PozycjeNaStrone = WielkoscStrony,
WszystkiePozycje = ListaZlecenWszystkich.Count()
},
ListaZlecenUzytkownika = ListaZlecen
};
return View(viewodel);
}
and View
#model AplikacjaHelpDesk.ViewModels.ListaZlecenUzytkownikaViewModel
#using AplikacjaHelpDesk.Infrastructure;
#{
ViewBag.Title = "Lista Zlecen Użytkownika";
Layout = "~/Views/Shared/_LayoutAdministracja.cshtml";
}
<div class="container-fluid">
<img src="~/Content/Images/Layout/Home.png" />
<a href="link">
#Html.MvcSiteMap().SiteMapPath()
</a>
<h2><span class="glyphicon glyphicon-user"></span>&nbsp<strong>Lista Zleceń </strong></h2>
<br /><br />
<div id="divLoading" class="panel panel-primary text-center text-primary" style="display:none;">
<h3><strong>Proszę czekać ładowane są posty!</strong></h3>
</div>
<div id="divLoadingForm" class="panel panel-primary text-center text-primary" style="display:none;">
<h3><strong>Proszę czekać ładuję formularz</strong></h3>
</div>
#*#if (ViewBag.Informacja != null)
{
<div class="alert alert-warning"><h4><strong>#TempData["Dodano-Post"]</strong></h4></div>
}*#
<table class="table table-responsive table-striped transactions" style="text-combine-upright:all;">
<tr style="text-transform: uppercase; text-combine-upright:all;">
<th>
<label>Pozycja Nr.</label>
</th>
<th>
<label>Nr Zlecenia</label>
</th>
<th>
<label>Data Przyjęcia Zlecenia</label>
</th>
<th>
<label>Data Planowanego Zakończenia Zlecenia</label>
</th>
<th>
<label>Data zakończenia zlecenia</label>
</th>
<th style="width: 160px;"></th>
<th style="width: 160px;"></th>
</tr>
#{ var i = 0;}
#foreach (var item in Model.ListaZlecenUzytkownika)
{
<tr class="panel panel-primary">
#*
<h5><strong>Zlecenie nr: #Html.DisplayFor(modeItem => item.IdZlecenia)</strong></h5>
<td>
#{i++;}
#i
</td>
<td>
<h5><strong>Zlecenie nr: #Html.DisplayFor(modeItem => item.IdZlecenia)</strong></h5>
</td>
<td>
#Html.DisplayFor(modelItem => item.DataPrzyjeciaZlecenia)
</td>
<td>
#Html.DisplayFor(modelItem => item.DataPlanowaniaZakonczenia)
</td>
<td>
#Html.DisplayFor(modelItem => item.DataZakonczenia)
</td>
<td>
#Ajax.ActionLink("Pokaż Posty Zlecenia", "_ListaPostow", new { idZlecenia = #item.IdZlecenia }, new AjaxOptions()
{
HttpMethod = "GET",
LoadingElementId = "divLoading",
UpdateTargetId = "divPozycje",
InsertionMode = InsertionMode.Replace
}, new { #class = "btn btn-primary" })
</td>
<td>
#Ajax.ActionLink("Dodaj Odpowiedz", "_DodajPost", new { idZlecenia = #item.IdZlecenia }, new AjaxOptions()
{
HttpMethod = "GET",
LoadingElementId = "divLoadingForm",
UpdateTargetId = "divDodajPozycje",
InsertionMode = InsertionMode.Replace
}, new { #class = "btn btn-primary" })
</td>
</tr>
<tr style="background: #23527c; color:white;">
<td>
<label>Opis załącznika</label>
</td>
<td style="width: 120px;">
<label>Załącznik</label>
</td>
</tr>
<tr class="panel panel-group">
<td>
#Html.Raw(item.ZleceniaZalaczniki.Opis)
</td>
<td>
<span class="btn btn-primary">
#Html.ActionLink("Pobierz", "Download", "Zlecenia", new { nazwaPliku = #item.ZleceniaZalaczniki.NazwaPliku }, null)
<span class="glyphicon glyphicon-download" aria-hidden="true"></span>
</span>
</td>
</tr>
<tr id="divDodajPozycje"></tr>
}
</table>
<br />
<div class="btn-group pull-right">
#Html.LinkStrony(Model.StronaInfo, x => Url.Action("ListaZlecen", new { strona = x }))
</div>
<br />
<hr />
<div id="divPozycje">
</div>
</div>
I tried this way but it creates numbering only on one page. Going to the next numbering is created from the beginning.
#{ var i = 0;}
<td>
#{i++;}
#i
</td>
I have 3 orders on each page and I would like all orders to be automatically numbered, for example:
First page 1,2,3
Second page 4,5,6
I am asking for help in creating the numbering for the entire collection of orders
Instead of starting at i=0 you can start at your current page,
I don't know which language it is but I hope i got it right.
// Instead of this: #{ var i = 0;} - on your view
//Use this:
#{ var i = (Model.AktualnaStrona -1 ) * Model.PozycjeNaStrone ;}
//It should be equal to the calculation you use on the API:
(strona - 1) * WielkoscStrony
//(from this line: )
var ListaZlecen = db.Zlecenia.OrderBy(w => w.IdZlecenia).
Skip((strona - 1) * WielkoscStrony). //--> THIS
Take(WielkoscStrony).ToList();

View Multiple Model that JOIN with FK through query from Controller

Here is Controller Code
//Approved
public ActionResult Approved(String VoucherNIK, String Admin, int? month, int? Year, int? minPrice, int? maxPrice)
{
if (Session["Nama_Lengkap"] != null)
{
var NIKLst = new List<int>();
var NIKQry = from d in db.Voucher
where d.Voucher_Number >= 2000
orderby d.NIK
select d.NIK;
var query = from app in db.Approved
join det in db.Detail on app.ID_Approved equals det.ID_Approve
join vou in db.Voucher on det.ID_Voucher equals vou.ID_Voucher
join adm in db.Admin on app.ID_Admin equals adm.ID_Admin
select new
{
vou.NIK,
app.Tanggal_Approve,
app.Total,
app.ID_Admin,
adm.Nama_Lengkap,
app.ID_Approved
};
IEnumerable < int > enumerable = NIKQry.GroupBy(v => v).Select(group => group.FirstOrDefault());
NIKLst = enumerable.ToList();
ViewBag.VoucherNIK = new SelectList(NIKLst);
try
{
if (!string.IsNullOrEmpty(Admin))
{
query = query.Where(s => s.Nama_Lengkap.Contains(Admin));
}
}
catch (DataException /* dex */)
{
ModelState.AddModelError("", "Unable to Apply.");
}
return View(query));
}
else
{
return RedirectToAction("Login", "Account");
}
}
And the View
#model IEnumerable<Voucher.Models.Approved>
#{
ViewBag.Title = "Approved";
Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}
#using (Html.BeginForm("Approved", "Voucher", FormMethod.Get))
{
<div class="form-horizontal">
<hr />
<div class="form-group">
NIK Voucher :
<div class="col-md-10">
#Html.DropDownList("VoucherNIK", null, "All") <!-- new { onchange = "document.location.href = '?VoucherNIK=' + this.value;" })!-->
</div>
</div>
<div class="form-group">
ADMIN Approve :
<div class="col-md-10">
#Html.TextBox("Admin")
</div>
</div>
<div class="form-group">
Month :
<div class="col-md-10">
#Html.TextBox("month")
</div>
</div>
<div class="form-group">
Year :
<div class="col-md-10">
#Html.TextBox("Year")
</div>
</div>
<div class="form-group">
Price(min NUMBER) :
<div class="col-md-10">
#Html.TextBox("minPrice")
</div>
</div>
<div class="form-group">
Price(max NUMBER) :
<div class="col-md-10">
#Html.TextBox("maxPrice")
</div>
</div>
</div>
<input type="submit" value="Filter" class="btn-toolbar" /><br />
}
<br />
<table class="table">
<tr>
<th>
Nama Admin
</th>
<th>
ID Approved
</th>
<th>
Tanggal
</th>
<th>
ID Admin
</th>
<th>
TOTAL
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Nama_Lengkap)
</td>
<td>
#Html.DisplayFor(modelItem => item.ID_Approved)
</td>
<td>
#Html.DisplayFor(modelItem => item.Tanggal_Approve)
</td>
<td>
#Html.DisplayFor(modelItem => item.ID_Admin)
</td>
<td>
#Html.DisplayFor(modelItem => item.Total)
</td>
<td>
#Html.ActionLink("More Detail", "DetailAppr", new { id = item.ID_Approved })
</td>
</tr>
}
</table>
When Run i got this error
The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery1
,but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[MvcMarks.Models.MarksType]'
Change return statement to
return View(query
.Select(it => new MarksType{
//initilize properties of `MarksType` with `it`
}));
Your view expects a model of type IEnumerable<MarksType> but query variable is IEnumerable of Anonymous objects.

Why httpPost model always null?

I have get and post controller.
But by httpPost comtroller passing model parameter values are null.
Why my httpPost model parameter values always null ??
[HttpGet]
public ActionResult HireItem()
{
var HireItemListModel = new HireItemModel();
HireItemListModel = new HireItemModel()
{
first_name = Session["first_name"].ToString(),
middle_name = Session["middle_name"].ToString(),
last_name = Session["last_name"].ToString(),
ceremony_date = Session["ceremony_date"].ToString(),
};
var product = _productService.GetAllHireProducts();
if (product.Count != 0)
{
foreach (var proValue in product)
{
var productVarSeparateList = _productService.GetHireProductVariantsByProductIds(proValue.Id, false);
foreach (var HireProSep in productVarSeparateList)
{
var productVarSeparateModel = new HireItemModel.HireItemSeparatetModel()
{
pname = HireProSep.Name,
price =HireProSep.Price,
pId=HireProSep.Id,
};
HireItemListModel.HireItemSeparatetlist.Add(productVarSeparateModel);
}
var productVarSetList = _productService.GetHireProductVariantsByProductIds(proValue.Id, true);
foreach (var HireProset in productVarSetList)
{
var productVarListset = new HireItemModel.HireItemSetModel()
{
pname = HireProset.Name,
price = HireProset.Price,
pId = HireProset.Id,
};
HireItemListModel.HireItemSetList.Add(productVarListset);
}
}
}
return View(HireItemListModel);
}
This controller HireItemModel model parameter values are null. WHY??
[HttpPost,ActionName("HireItem")]
public ActionResult HireItem(string submitB, FormCollection formCollection, HireItemModel HireItemListModel)
{
var graduandList = _graduandService.GetGraduandBynameCeremony(HireItemListModel.ceremony_id, HireItemListModel.first_name, HireItemListModel.middle_name, HireItemListModel.last_name);
foreach (var graduand in graduandList)
{
graduand.height = HireItemListModel.height;
graduand.head_circumference = HireItemListModel.head_circumferenc;
_graduandService.Updategraduand(graduand);
}
this is my view.
#model HireItemModel
#using (Html.BeginForm())
{
<table >
<tr>
<td >
Ceremony :
</td>
<td>
Ceremony at #Model.ceremony_date
</td>
</tr>
<tr>
<td >
Name :
</td>
<td >
#Model.first_name #Model.middle_name #Model.last_name
</td>
</tr>
</table>
<div id="HItemType_1">
#Html.CheckBox("HItemType")
#*<input type="checkbox" name="test" value="test" id="HItemType" />*#
<label> Academic Dress Set</label>
</div>
<div id="HsetItem">
#Html.Partial("_LoadHireSetItem", #Model.HireItemSetList)
</div>
<div id="HseparateItem">
#Html.Partial("_LoadHireSeparateItem", #Model.HireItemSeparatetlist)
</div>
<table >
<tr>
<td colspan="2">
Please tell us your measurement:
</td>
</tr>
<tr>
<td >
Height (in cm):
</td>
<td>
#Html.EditorFor(model => model.height)
</td>
</tr>
<tr>
<td >
Head circumference (in cm):
</td>
<td >
#Html.EditorFor(model => model.head_circumferenc)
</td>
</tr>
</table>
<div>
<input class="productlistaddtocartbutton" type="submit" value="Add to cart" name="submitB" id="btnaddtocart"/>
</div>
}
thanks.
Make sure inside your view you have input fields for all values that you intend to use in your POST action. For example if you want to use the ceremony_id, first_name, middle_name, and last_name properties you need to include them:
#Html.HiddenFor(model => model.ceremony_id)
#Html.HiddenFor(model => model.first_name)
#Html.HiddenFor(model => model.middle_name)
#Html.HiddenFor(model => model.last_name)
You could use hidden fields if the user is not supposed to modify their values but you could also have used text fields depending on your requirements.

Categories