'Model' conflicts with the declaration 'System.Web.Mvc.WebViewPage<TModel>.Model' - asp.net-mvc

I am having a problem in my code and I can not use a list inside a class in a foreach, has anyone had a similar problem?
Follows the code:
Model
public class Detalhes_Pedido
{
public Pedidos Pedido { get; set; }
public Pedidos_Endereco Pedido_Endereco { get; set; }
public Pedidos_Status Pedido_Status { get; set; }
public List<Pedidos_Produtos> Pedido_Produto { get; set; }
public List<Produtos> Produto { get; set; }
public Clientes Cliente { get; set; }
public Metodos_Entrega Metodo_entrega { get; set; }
public Metodos_Pagamento Metodos_Pagamento { get; set; }
}
View
#model Ecommerce.Models.Repository.Pedido_Repository.Detalhes_Pedido
#Html.AntiForgeryToken()
#Html.HiddenFor(model => model.Pedido.ID)
<div class="module_content">
<div class="ModuleConteiner">
#Html.ValidationSummary(true)
<table class="QuatroColumnTable">
<tr>
<td colspan="2">
<h3>Dados da Transação</h3>
<br />
</td>
</tr>
<tr>
<td>
#Html.LabelFor(model => model.Pedido.ID, "Número do Pedido: ")
<br />
</td>
here is where the problem occurs:
<td>
#Html.DisplayFor(model => model.Pedido.ID)
<br />
</td>
</tr>
<tr>
<td>
#Html.LabelFor(model => model.Pedido, "Peso total da compra: ")
</td>
<td>
**#foreach (var item in model.Produto)
{
}**
</td>
</tr>
</table>
What can be happening?

You need a capital M in Model:
#foreach (var item in Model.Produto)
{
}

Related

Check radio button based on database value

I have a table of Dues and I wanted to select radio button based on its database value either their dues cleared or not.
I have 3 different radio buttons which get inserted as int values in my database.
Dues:
public partial class Due
{
public int Id { get; set; }
public Nullable<int> DuesTypeId { get; set; }
[Required(ErrorMessage = "SF No is required")]
public Nullable<int> SF_No { get; set; }
[Required(ErrorMessage = "Amount is required")]
[DataType(DataType.Currency, ErrorMessage = "Only numbers can be enter")]
[DisplayFormat(DataFormatString = "{0:N2}")]
public Nullable<int> Amount { get; set; }
public Nullable<int> Status { get; set; }
public virtual DuesStatus DuesStatus { get; set; }
public virtual DuesType DuesType { get; set; }
public virtual Family Family { get; set; }
}
DuesStatus:
public partial class DuesStatus
{
public DuesStatus()
{
this.Dues = new HashSet<Due>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Due> Dues { get; set; }
}
View:
#model IEnumerable<PassAllocation.Models.Due>
#{
ViewBag.Title = "Index";
var status = ViewBag.Status as List<PassAllocation.Models.DuesStatus>;
}
<h3>Dues</h3>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
Families Dues
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<table width="100%" class="table table-striped table-bordered table-hover" id="familydues">
<thead>
<tr>
<th>SF No</th>
<th>Head of Family</th>
<th>Amount</th>
<th>Status</th>
</tr>
</thead>
<tfoot>
<tr>
<th>SF No</th>
<th>Head of Family</th>
<th>Amount</th>
<th>Status</th>
</tr>
</tfoot>
<tbody>
#foreach (var item in Model)
{
<tr id="#item.Id">
<td>
#Html.DisplayFor(modelItem => item.Family.SF_No)
</td>
<td>
#Html.DisplayFor(modelItem => item.Family.Members.Where(x => x.Member_Type == 1).FirstOrDefault().Full_Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Amount)
</td>
<td>
#foreach (var s in status)
{
<label class="Cont">
#Html.RadioButtonFor(m=>status,s.Id, new { #class = "Status ", #checked = "checked"}) #s.Name
<span class="checkmark"></span>
</label>
}
</td>
</tr>
}
</tbody>
</table>
</div>
<!-- /.panel-body -->
</div>
<!-- /.panel -->
</div>
<!-- /.col-lg-12 -->
I want my result like this but I am not able to select that radio button for which data is saved.
Use This trinary operator to handle the condition.
#Html.RadioButtonFor(m=>status,s.Id, Model.status== true ? new { Checked = "checked" ,#class = "Status "} : new { #class = "Status "})

Simple form submission gives missing resource on postback

Apologies if this seems mundane, but I am a huge beginner to MVC and this seemingly simple task is giving me a massive headache.
I can't understand why my code isn't working. I'm submitting a form from my Index.cshtml and the postback is telling me the resource /Index (the page I am posting from) doesn't exist.
Could I please get some help on what I am doing wrong?
My View (Index.cshtml):
#model MyProject.Connection
#{
ViewBag.Title = "Index";
}
<div id="container" class="container centered primary">
#using (Html.BeginForm(FormMethod.Post))
{
<img src="#Url.Content("/Content/images/default.png")" alt="CMGR Web" />
<div class="divider"></div>
<fieldset id="fs_server" class="borderless!T">
<legend>Server details</legend>
<table>
<tr>
<td>
#Html.LabelFor(c => c.serverHost)
</td>
<td>
#Html.TextBoxFor(c => c.serverHost)
</td>
</tr>
<tr>
<td>
#Html.LabelFor(c => c.instanceName)
</td>
<td>
#Html.TextBoxFor(c => c.instanceName)
</td>
</tr>
</table>
</fieldset>
<fieldset id="fs_user" class="borderless!T">
<legend>Your credentials</legend>
<table>
<tr>
<td>
#Html.LabelFor(c => c.username)
</td>
<td>
#Html.TextBoxFor(c => c.username)
</td>
</tr>
<tr>
<td>
#Html.LabelFor(c => c.password)
</td>
<td>
#Html.PasswordFor(c => c.password)
</td>
</tr>
</table>
</fieldset>
<div class="divider"></div>
<table>
<tr>
<td>
#Html.CheckBoxFor(c => c.remember)
#Html.Label("Remember me?")
</td>
<td>
<input type="submit"/>
</td>
</tr>
</table>
}
</div>
My Model (Connection.cs):
namespace MyProject.Models
{
public class Connection
{
[Display(Name="Server Host")]
public string serverHost { get; set; }
[Display(Name="Instance Name")]
public string instanceName { get; set; }
[Display(Name = "Username")]
public string username { get; set; }
[Display(Name = "Password")]
public string password { get; set; }
[Display(Name = "Remoting Password")]
public string remotingPassword { get; set; }
[Display(Name = "Persistent")]
public bool remember { get; set; }
}
}
My Controller (IndexController.cs)
namespace MyProject.Controllers
{
public class IndexController : Controller
{
//
// GET: /Login/
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
private ActionResult Index(Connection channel)
{
return View();
}
}
}
Your Post ActionResult is set to Private so it isnt accessible. Change it to public
private ActionResult Index(Connection channel)
{
return View();
}
public ActionResult Index(Connection channel)
{
return View();
}

Create table based on two nested foreach loops in MVC Razor

I tried to create a week view table by using two nested foreach loop,
Model:
public class EmployeeViewModel
{
public ServiceProvider Employee { get; set; }
public IEnumerable<DayViewModel> Days { get; set; }
}
public class DayViewModel
{
public DateTime TheDate { get; set; }
public TimePeriodCollection FreeTimes { get; set; }
public IEnumerable<DateTime> Blocks { get; set; }
}
So I have a list of dayes and each days has a list of times
In the view:
#model BookingSystem.ViewModels.EmployeeViewModel
:
<div class="table-responsive">
<table class="table table-bordered table-striped table-hover">
#foreach (var item in Model.Days)
{
<th class="text-center">
#item.TheDate.ToString("dddd") <br /> #item.TheDate.ToString("MM-dd")
</th>
}
<tr>
#foreach (var item in Model.Days)
{
<td class="text-center col-sm-15">
#if (item.FreeTimes != null)
{
foreach (var iu in item.FreeTimes)
{
<span>
#Html.ActionLink(iu.Start.ToString("HH:mm") + " - " + iu.End.ToString("HH:mm"), "Index", "ClientForm", new { ChoosedStartTime = iu.Start.ToString("HH:mm"), ChoosedEndTime = iu.End.ToString("HH:mm"), ChoosedDate = item.TheDate }, new { #class = "btn btn-primary" })
</span>
<br />
}
}
</td>
}
</tr>
</table>
</div>
I had an acceptable result but what I want is: each time block to be in a separate row so I can use table-striped class and put borders.
This is what I did:
Image of the result
Any Ideas.
A Quick Solution would be to control by a variable.
Let's say you have Treatments and Appointments for a given Patient. It would be something like
public class Patient: Person
{
public virtual ICollection<Treatment>? Treatments { get; set; }
}
And a Patient as multiple Treatments
public class Treatment
{
public int Id { get; set; }
[DisplayName("Treatment Type")]
public TreatmentType TreatmentType { get; set; }
[DisplayName("Upfront Cost (INR)")]
public decimal? UpFrontCost { get; set; } = 0;
[DisplayName("Total Cost (INR)")]
public decimal? TotalCost { get; set; } = 0;
public virtual ICollection<Appointment>? Appointments { get; set; }
public int PatientId { get; set; }
public virtual Patient? Patient { get; set; }
}
And each treatment would have multiple Appointments.
public class Appointment
{
public int Id { get; set; }
[DisplayName("Appointment Time")]
public DateTime AppointmentTime { get; set; }
[DisplayName("Appointment Status")]
public AppointmentStatus AppointmentStatus { get; set; }
[DisplayName("Charge (INR)")]
public decimal? Cost { get; set; } = 0;
[ForeignKey(nameof(Treatment))]
[DisplayName(nameof(Treatment))]
public virtual int TreatmentId { get; set; }
public virtual Treatment? Treatment { get; set; }
}
Use this approach to have nested models rendered using #foreach in razor views.
<div class="row flex-row">
<h4> #Html.DisplayNameFor(model => model.Treatments)</h4>
<hr />
#if (Model.Treatments.Count == 0)
{
<p>
Treatment not started. <a asp-controller="Treatments" asp-action="Create" asp-route-patientId="#Model.Id">Start treatment</a> for this paitent!
</p>
}
else
{
<table class="table table-striped">
#{
var trHead = 0;
var apHead = 0;
}
#foreach (var tmnt in Model.Treatments)
{
if (trHead == 0)
{
<thead>
<tr>
<th>
#Html.DisplayNameFor(a => tmnt.TreatmentType)
</th>
<th>
#Html.DisplayNameFor(a => tmnt.TotalCost)
</th>
<th></th>
</tr>
</thead>
trHead += 1;
}
<tbody>
#{
<tr>
<td>
#Html.DisplayFor(a => tmnt.TreatmentType)
</td>
<td>
#Html.DisplayFor(a => tmnt.TotalCost)
</td>
<td>
<a class="float-end" asp-controller="Treatments" asp-action="Details" asp-route-id="#tmnt.Id">View treatment</a>
</td>
</tr>
apHead = 0;
}
<tr>
<td colspan="4">
#if (tmnt.Appointments.Count == 0)
{
<p>
No Appointments Scheduled. <a asp-controller="Appointments" asp-action="Create" asp-route-treatmentId="#tmnt.Id">Schedule an appointment</a> for this paitent!
</p>
}
else
{
<table class="table mb-0">
#foreach (var apt in tmnt.Appointments)
{
if (apHead == 0)
{
<thead>
<tr>
<th>
#Html.DisplayNameFor(a => apt.AppointmentTime)
</th>
<th>
#Html.DisplayNameFor(a => apt.AppointmentStatus)
</th>
<th>
#Html.DisplayNameFor(a => apt.Cost)
</th>
<th></th>
</tr>
</thead>
apHead += 1;
}
<tbody>
<tr>
<td>
#Html.DisplayFor(a => apt.AppointmentTime)
</td>
<td>
#Html.DisplayFor(a => apt.AppointmentStatus)
</td>
<td>
#Html.DisplayFor(a => apt.Cost)
</td>
<td>
<a class="float-end" asp-controller="Appointments" asp-action="Details" asp-route-id="#apt.Id">View Appointment</a>
</td>
</tr>
</tbody>
}
</table>
}
</td>
</tr>
</tbody>
}
</table>
}
</div>
Then, obtain the following output
Responsive mobile view (scope for some improvement though! :p
PS: using Bootstrap 5.1
References: https://getbootstrap.com/docs/5.0/content/tables/#nesting

Using an additional table in a MVVM Details View

at the moment I have a OrderConfirmationController which has a Details view which displays the records. At the bottom of the Details View I want to add 5 fields from another table that will be displayed in the view but not sure how to add them.
At the bottom of the last table I want to implement a separate table containing the Product_ID, Description, Price, Quantity and Total from a Sales_Order_Line table but don't know how to add it as I can only reference one table using the #model at the start of the view.
Thanks.
Here is my code for the new class I have created:
class OrderConfirmation
{
Sales_Order Bill_to_Customer_Name { get; set; }
Sales_Order Bill_to_Address { get; set; }
Sales_Order Bill_to_Address_2 { get; set; }
Sales_Order Bill_to_City { get; set; }
Sales_Order Bill_to_County { get; set; }
Sales_Order Bill_to_Post_Code { get; set; }
Sales_Order Bill_to_Country_Region_Code { get; set; }
Sales_Order Ship_to_Contact_Name { get; set; }
Sales_Order Ship_to_Address { get; set; }
Sales_Order Ship_to_Address_2 { get; set; }
Sales_Order Ship_to_City { get; set; }
Sales_Order Ship_to_County { get; set; }
Sales_Order Ship_to_Post_Code { get; set; }
Sales_Order Ship_to_Country_Region_Code { get; set; }
Sales_Order_Line Product_ID { get; set; }
Sales_Order_Line Description { get; set; }
Sales_Order_Line Price { get; set; }
Sales_Order_Line Quantity { get; set; }
Sales_Order_Line Total { get; set; }
}
Here is my code for the Details controller:
public ActionResult Details(string documentNo, int documentType)
{
ViewBag.Sales_Order_Line = Sales_Order_Line.Product_ID;
ViewBag.Sales_Order_Line = Sales_Order_Line.Description;
ViewBag.Sales_Order_Line = Sales_Order_Line.Price;
ViewBag.Sales_Order_Line = Sales_Order_Line.Quantity;
ViewBag.Sales_Order_Line = Sales_Order_Line.Total;
// Compound key is used here so require both fields to be included in the SQL.
Sales_Order confirmOrder = _data.Sales_Orders
.Where(x => x.Document_Type == documentType && x.Document_No_ == documentNo)
.FirstOrDefault();
return View(confirmOrder);
}
And this is my View:
#model SPR.Titanium.MultiChannel.ManagementConsolePortal.Models.OrderConfirmation
<link href="~/Content/themes/ui-lightness/jquery-ui-1.10.3.custom.css" rel="stylesheet" />
#section Scripts
{
#Scripts.Render("~/bundles/jqueryui");
<script src="~/js/jquery-1.9.1.js"></script>
<script src="~/js/jquery-ui-1.10.3.custom.js"></script>
<script>
$(function () {
$("#accordion").accordion();
})
</script>
}
#{
ViewBag.Title = "Order Confirmation Details";
}
<h2>Order Confirmation</h2>
#using (Html.BeginForm())
{
<div id="accordion">
<div class="editor-field">
Billing Details:
</div>
<div class="editor-field">
<table style="width:100%">
<tr>
<th style="width:20%">Billing Name</th>
<td style="width:30%">#Html.EditorFor(model => model.Bill_to_Customer_Name)</td>
<th style="width:20%">Address</th>
<td style="width:30%">#Html.EditorFor(model => model.Bill_to_Address).</td>
<th style="width:20%">Address 2</th>
<td style="width:30%">#Html.EditorFor(model => model.Bill_to_Address_2).</td>
</tr>
<tr>
<th style="width:20%">City</th>
<td style="width:30%">#Html.EditorFor(model => model.Bill_to_City)</td>
<th style="width:20%">County</th>
<td style="width:30%">#Html.EditorFor(model => model.Bill_to_County).</td>
<th style="width:20%">Post Code</th>
<td style="width:30%">#Html.EditorFor(model => model.Bill_to_Post_Code)</td>
</tr>
<tr>
<th style="width:20%">Country</th>
<td style="width:30%">#Html.EditorFor(model => model.Bill_to_Country_Region_Code)</td>
</tr>
</table>
</div>
<div class="editor-label">
Delivery Details
</div>
<div class="editor-field" aria-readonly="true">
<table style="width:100%">
<tr>
<th style="width:20%">Delivery Name</th>
<td style="width:30%">#Html.EditorFor(model => model.Ship_to_Contact_Name)</td>
<th style="width:20%">Address</th>
<td style="width:30%">#Html.EditorFor(model => model.Ship_to_Address)</td>
<th style="width:20%">Address 2</th>
<td style="width:30%">#Html.EditorFor(model => model.Ship_to_Address_2)</td>
</tr>
<tr>
<th style="width:20%">City</th>
<td style="width:30%">#Html.EditorFor(model => model.Ship_to_City)</td>
<th style="width:20%">County</th>
<td style="width:30%">#Html.EditorFor(model => model.Ship_to_County)</td>
<th style="width:20%">Post Code</th>
<td style="width:30%">#Html.EditorFor(model => model.Ship_to_Post_Code)</td>
</tr>
<tr>
<th style="width:20%">Country</th>
<td style="width:30%">#Html.EditorFor(model => model.Ship_to_Country_Region_Code)</td>
</tr>
</table>
</div>
<div>
#{
var line = (Sales_Order_Line)ViewBag.Sales_Order_Line;
</div>
</div>
}
<div>
#Html.ActionLink("Return to Sales Order List", "Index")
</div>
I just noticed that my first idea wouldn't work as I imagined. But you can use the ViewBag to pass additional values to the view:
Create a new class that has two properties, one of type Sales_Order and one of a class that contains the data from your other table. Use this new class as your #model and you can access them both.
class DetailsViewModel
{
Sales_Order SalesOrder { get; set; }
Sales_Order_Line { get; set; }
}
In your controller:
public ActionResult Details(string documentNo, int documentType)
{
ViewBag.Sales_Order_Line = // get Sales_Order_Line object here
... // the rest of the code as before
}
In your view:
<div>
#{
var line = (Sales_Order_Line)ViewBag.Sales_Order_Line;
...
}
</div>

How to link a list table view to another table view for details using ASP.NET MVC

I am viewing a list on the 1st page and on the 2nd page it should link using the same id from the database on the 1st page, but the 2nd page its details page.
1st page - listview
2nd page - details
Here is what I have already tried
Example: http://abctutorial.com/Post/53/mvc5-master-detail-edit-using-aspnet--jquery--razor
The Model called: "Ordering" and its display on the ListView
public Ordering()
{
this.Invoice_Line_Item = new HashSet<Invoice_Line_Items>();
}
[Key]
public int order_id { get; set; }
public Guid? CustomerId { get; set; }
public int? CustId { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
[StringLength(34)]
public string invoice_number { get; set; }
[StringLength(200)]
public string EmailId { get; set; }
[StringLength(50)]
public string ClientFirstname { get; set; }
[StringLength(50)]
public string ClientLastname { get; set; }
[StringLength(50)]
public string MobileNumber { get; set; }
[StringLength(50)]
public string PaymentStatus { get; set; }
[StringLength(50)]
public string trackingorderno { get; set; }
[StringLength(50)]
public string Status { get; set; }
[StringLength(200)]
public string DeliveryNote { get; set; }
[StringLength(250)]
public string Agent { get; set; }
public DateTime? date_order_placed { get; set; }
public virtual ICollection<Invoice_Line_Items> Invoice_Line_Item {
get; set; }
ListView page for "Ordering"
#model IEnumerable<LifestyleAdminOriginal.Models.Ordering>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.invoice_number)
</td>
<td>
#Html.DisplayFor(modelItem => item.ClientFirstname) #Html.DisplayFor(modelItem => item.ClientLastname)
</td>
<td>
#Html.DisplayFor(modelItem => item.EmailId)
</td>
<td>
#Html.DisplayFor(modelItem => item.MobileNumber)
</td>
<td>
<span class="label label-danger">#Html.DisplayFor(modelItem => item.Status)</span>
</td>
<td>
#Html.ActionLink("View", "NewOrdersDetails", new { id = item.CustId })
</td>
</tr>
}
Details page for "Invoice_Line_Items"
<div class="row">
#if (Model.Count() != 0)
{
foreach (var item in Model)
{
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="invoice-sp">
<table class="table table-hover">
<thead>
<tr>
<th>Service</th>
<th>Item</th>
<th>Gender</th>
<th>Unit Price</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
#foreach (var inv in item.Invoice_Line_Item)
{
<th>#inv.service</th>
<td>#inv.item</td>
<td>#inv.gender</td>
<td>#inv.price</td>
<td>#inv.quantity</td>
<td>#inv.price</td>
}
</tr>
</tbody>
</table>
</div>
</div>
}
}
</div>
Listview Controller for "Ordering"
public ActionResult NewOrders()
{
var count = db.Orderings.Where(s => s.trackingorderno == "New Order").Count();
ViewBag.totalall = count;
return View(db.Orderings.ToList().Where(x => x.trackingorderno == "New Order").Select(x => x));
}
Detailsview Controller for "Invoice_Line_Items" and "Ordering"
public ActionResult NewOrdersDetails(int? CustId)
{
if (CustId == null)
{
return new System.Web.Mvc.HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
List<Ordering> OrderAndCustomerList = db.Orderings.ToList();
if (OrderAndCustomerList == null)
{
return HttpNotFound();
}
return View(OrderAndCustomerList);
}
Basically, whenever a user clicks "View" on listview page, it should take them to the details page where it shows the invoice line items and displays the customer details as well on one page which is "DetailsView"
In First table Where You Show Main Data and Second Table Detail Of the First table data
so you have to take detail table content id in Main Table content and send it to second page with detail id from main table.
I managed to get the answer from the following example on the below link. I used the ICollection on the model Ordering
https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application
**Ordering Model**
[ForeignKey("order_id")]
public virtual ICollection<Invoice_Line_Items> Invoice_Line_Item { get; set; }
**Invoice_Line_Items Model**
public virtual Ordering Ordering { get; set; }
**Details Page**
<table class="table">
<tr>
<th>Service</th>
<th>Department</th>
</tr>
#foreach (var item in Model.Invoice_Line_Item)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.invoice_number)
</td>
<td>
#Html.DisplayFor(modelItem => item.service)
</td>
<td>
#Html.DisplayFor(modelItem => item.department)
</td>
<td>
#Html.DisplayFor(modelItem => item.item)
</td>
<td>
#Html.DisplayFor(modelItem => item.quantity)
</td>
<td>
#Html.DisplayFor(modelItem => item.price)
</td>
<td>
#Html.DisplayFor(modelItem => item.vat)
</td>
<td>
#Html.DisplayFor(modelItem => item.grandtotal)
</td>
</tr>
}
</table>

Resources