ASP.NET MVC web app - Error 404 when accessing Edit, Details, Delete Views - asp.net-mvc

I am creating a web application for my database. I have inserted the model and have scaffolded items for each of my tables.
What's weird about this is that my 'Create' works perfectly. However I cannot get the edit, details or delete to work. I just get the error
HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
See the example: I have this table:
Table Example
I have the following code for this page:
#model IEnumerable<WebApplication5.Models.SafetyValveData>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Batch_No)
</th>
<th>
#Html.DisplayNameFor(model => model.SafetyValvePartNo)
</th>
<th>
#Html.DisplayNameFor(model => model.BAR)
</th>
<th>
#Html.DisplayNameFor(model => model.JobNo)
</th>
<th>
#Html.DisplayNameFor(model => model.Date_Of_Manufacture)
</th>
<th>
#Html.DisplayNameFor(model => model.Certificate)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Batch_No)
</td>
<td>
#Html.DisplayFor(modelItem => item.SafetyValvePartNo)
</td>
<td>
#Html.DisplayFor(modelItem => item.BAR)
</td>
<td>
#Html.DisplayFor(modelItem => item.JobNo)
</td>
<td>
#Html.DisplayFor(modelItem => item.Date_Of_Manufacture)
</td>
<td>
#Html.DisplayFor(modelItem => item.Certificate)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.Batch_No }) |
#Html.ActionLink("Details", "Details", new { id=item.Batch_No }) |
#Html.ActionLink("Delete", "Delete", new { id=item.Batch_No })
</td>
</tr>
}
</table>
Below is the example of the "Edit" code for this table.
#model WebApplication5.Models.SafetyValveData
#{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>SafetyValveData</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.Batch_No)
<div class="form-group">
#Html.LabelFor(model => model.SafetyValvePartNo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.SafetyValvePartNo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.SafetyValvePartNo, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.BAR, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.BAR, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.BAR, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.JobNo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.JobNo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.JobNo, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Date_Of_Manufacture, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Date_Of_Manufacture, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Date_Of_Manufacture, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Certificate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Certificate, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Certificate, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
EDIT
This is the URL it tries to access when editing data from this table:
http://localhost:38636/SafetyValveDatas/Edit/334609%20%20%20%20

Related

Calling a Partialview with table on the main view

I'm trying to call a partial view with details table for some items. However, the partial view does not show any record in the main view. How I can call the Patrial view successfully? the partial view as follows:
#model IDECOHealthInsurance.Models.Pharmacy
#using (Html.BeginForm("pharmacyDetials", "Pharmacy"))
{
<h4>تفاصيل الصيدلية</h4>
<div id="dvPatientNotice" class="MainGridContainer pb-5">
#if (Model.dtItemsDetails != null)
{
<table dir="rtl" id="Paitents" class="MainGrid">
<thead>
<tr style="text-size-adjust:auto">
<th>
رقم الموظف
</th>
<th>
التاريخ
</th>
<th>
الوقت
</th>
<th>
المستفيدون
</th>
<th>
ملاحظات
</th>
<th>
الباركورد
</th>
<th>
أسم العينة
</th>
<th>
الكمية
</th>
<th>
السعر
</th>
</tr>
</thead>
<tbody>
#foreach (System.Data.DataRow row in Model.dtItemsDetails.Rows)
{
<tr style="width:100%">
<td>
#row["EMPLOYEE_NUMBER"]
</td>
<td>
#row["ENTRY_DATE"]
</td>
<td>
#row["ENTRY_TIME"]
</td>
<td>
#row["BENEFICIARIES"]
</td>
<td>
#row["NOTE"]
</td>
<td>
#row["ITEM_CODE"]
</td>
<td>
#row["ITEM_NAME"]
</td>
<td>
#row["QTY"]
</td>
<td>
#row["PRICE"]
</td>
</tr>
}
</tbody>
</table>
}
</div>
}
The controller as follows:
[HttpGet]
public ActionResult pharmacyDetials(Pharmacy model)
{
var masterID = Convert.ToInt32(Session["login"]);
if (masterID == 0)
{
return RedirectToAction("Login");
}
else
{
Models.Pharmacy objPharamcyMode = new Pharmacy();
IDECOServiceReference.IdecoAPIServiceClient idecoAPI = new IDECOServiceReference.IdecoAPIServiceClient();
DataTable dataTable = idecoAPI.GETPHARMACYEMPLOYEEMASTER("", 1);
model.dtItemsDetails = dataTable;
return PartialView("_PharmacyDetails", model);
}
}
And the Main view as follows:
#model IDECOHealthInsurance.Models.Pharmacy
#{
ViewBag.Title = "PharmacyApplication";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<table style="height:680px; width:1280px; border:hidden">
<tr>
<td>
<div id="pDetail">
#Html.Partial("_PharmacyDetails", Model)
</div>
</td>
<td>
#using (Ajax.BeginForm("PharmacyApplication", "Pharmacy", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "updatePnl", InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace, LoadingElementId = "Loading", OnBegin = "" }))
{
<div class="form-horizontal">
<div class="form-group">
#Html.LabelFor(model => model.PHARMACY_NAME, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DisplayFor(model => model.PHARMACY_NAME)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.EMPLOYEE_NUMBER, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.EMPLOYEE_NUMBER, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.EMPLOYEE_NUMBER, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ENTRY_DATE, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DisplayFor(model => model.ENTRY_DATE, new { htmlAttributes = new { #class = "form-control", #Value = DateTime.Today.ToString("dd/MM/yyyy"), #readonly = "readonly" } })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ENTRY_TIME, new { htmlAttributes = new { #class = "form-control", #Value = DateTime.Today.ToString("HH:mm:ss"), #readonly = "readonly" } })
<div class="col-md-10">
#Html.DisplayFor(model => model.ENTRY_TIME)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.BENEFICIARIES, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
زوجة #Html.RadioButtonFor(m => m.BENEFICIARIES, 1)
أبن #Html.RadioButtonFor(m => m.BENEFICIARIES, 2)
أبنة #Html.RadioButtonFor(m => m.BENEFICIARIES, 3)
الموظف #Html.RadioButtonFor(m => m.BENEFICIARIES, 4)
#Html.ValidationMessageFor(model => model.BENEFICIARIES, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Note, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<textarea name="NOTE" id="comments" style="font-family:'Times New Roman';font-size:1.2em; width: 280px; height:auto" placeholder="أكتب ملاحظاتك هنا"></textarea>
#Html.ValidationMessageFor(model => model.Note, "", new { #class = "text-danger" })
</div>
</div>
<div id="showPnl">
<input class="btn btn-default" type="button" value="تفاصيل الصيدلية" onclick="#("window.location.href='" + #Url.Action("pharmacyDetials", "Pharmacy") + "'");" />
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" id="panel" value="أضافة" class="btn btn-default" />
</div>
<input class="btn btn-default" type="button" value="خروج" onclick="#("window.location.href='" + #Url.Action("LogOut", "Pharmacy") + "'");" />
</div>
</div>
}
</td>
</tr>
</table>
<div id="updatePnl">
#Html.Partial("_PartialPharmacyDetails", Model)
</div>
<br />
<br />
<br />
<div id="pnlItemsDetails">
#Html.Partial("_PartialItemsDetails", Model)
</div>
When I click on the pharmacy details (in Arabic) = "تفصيل الصيدلية" button it redirects me to another page which contains the desired table that I want to show in the main view. However, I don't want this to happen I want to show the table without clicking on the button. Could you explain why this problem happened?
The Result at the run time appears as follows:
Result of the application at run time
If you want to prevent your page from redirecting, you've to use an ajax request. Replace below line :
<input class="btn btn-default" type="button" value="تفاصيل الصيدلية" onclick="#("window.location.href='" + #Url.Action("pharmacyDetials", "Pharmacy") + "'");" />
with :
<input class="btn btn-default" type="button" value="تفاصيل الصيدلية" onclick="getPharmacyDetails();" />
and in your javascript section, add below code :
function getPharmacyDetails()
{
$.ajax({
url : '#Url.Content("~/Pharmacy/pharmacyDetials")',
type: "GET",
success: function (result) {
$("#pDetail").html(result);
}
});
}
Also, replace below HTML :
<div id="pDetail">
#Html.Partial("_PharmacyDetails", Model)
</div>
With :
<div id="pDetail"></div>
The above ajax request will execute the partial view and fill the pDetial div with the required result. Try it and Let me know if you've any query.

Creating an edit modal in ASP.NET - MVC

I have Index.cshtml with the code below
#model IEnumerable<Demo.Models.RequestList>
....
#foreach (var item in Model)
{
<td>#Html.DisplayFor(modelItem => item.Name)</td>
...
<a class="dropdown-item" onclick="edit(#item.ID)" href="#Url.Action("Edit", "Home", new { id = item.ID })">Edit</a>
}
It mean, I get data from DB and show on the table. Each row of the table I create a button for edit.
When I click Edit button, it will href="#Url.Action("Edit", "Home", new { id = item.ID })
But now, I want create Edit modal or a #html.partial on the Modal instead of href to a new page. How can I do that?
It is my Edit code.
#model Demo.Models.RequestList
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>RequestList</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.ID)
<div class="form-group">
#Html.LabelFor(model => model.Code, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Code, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Code, "", new { #class = "text-danger" })
</div>
</div>
....
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>

MVC, Normal partial view and List partial view. not working together!!! S.O.S. xD

I have a view that contains 2 partial views.
#model ListViewModel
....
#{
Html.RenderPartial("EditCartItemsPartical");
Html.RenderPartial("ShowProductINFO", Model.Products);
}
and I just want to create a from with the first partial, and a list with the second.
The partial views
EditCartItemsPartical.cshtml
#model TestNewATM3._0.Models.CartItem
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<div class="form-group">
#Html.LabelFor(model => model.CartId, "CartId", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.CartId, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.CartId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ProductId, "ProductId", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ProductId, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ProductId, "", new { #class = "text-danger" })
</div>
</div>
.... // more control for CartItem
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
ShowProductINFO.cshtml
#model IEnumerable<TestNewATM3._0.Models.AllProduct2>
<p>#Html.ActionLink("Create New", "Create")</p>
<table class="table">
<tr>
<th>ID</th>
<th>#Html.DisplayNameFor(model => model.Title)</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>#Html.DisplayFor(model => item.Id)</td>
<td>#Html.DisplayFor(modelItem => item.Title)</td>
</tr>
}
</table>
And in my controller I got this.
public ActionResult Edit()
{
var db = ApplicationDbContext.Create();
var list = new ListViewModel
{
Products = db.AllProduct2s.ToList(),
Users = db.Users.ToList(),
Cart = db.Carts.ToList(),
CartItem = db.CartItems.ToList()
};
return View(list);
}
But the problem is that I cant show both partial at the same time because if I send the list in my view, then my first partial gets a problem cause it want a #model TestNewATM3.0.Models.CartItem. but if I don't sent the list My list wont show because I don't send it.
So how do i show a normal partial form view and a List partial view at the same time?
I'm having a little difficulty understanding your examples (i.e you have an edit view with create buttons), it looks like your trying to create a view to edit the cart.
Note: I would suggest renaming the CartItem property of your model to CartItems for clarity, but I'll use the current property names for the examples below.
You could render the partial view for each of the items in the list like so,
but this approach more is more than a little messy:
foreach(var cartItem in Models.CartItem){
Html.RenderPartial("EditCartItemsPartical", cartItem);
}
A simpler and more performant approach would be to edit the first view so it takes a collection of cart items
Like so
#model IEnumerable<TestNewATM3._0.Models.CartItem>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
#foreach(var modelItem in Model.CartItem){
<h4>CartItem</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => modelItem.CartId, "CartId", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => modelItem.CartId, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => modelItem.CartId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ProductId, "ProductId", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => modelItem.ProductId, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => modelItem.ProductId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Aantal, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => modelItem.Aantal, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => modelItem.Aantal, "", new { #class = "text-danger" })
</div>
</div>
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
Of course this will require you to change the server side code to receive a collection of cart items, but from the user's perspective not having to post a form back multiple times, to complete your changes is going to result in a better experience.
disclaimer: I changed the above view in notepad, might need to adjust a bit before it works perfectly
I hope this helps.

Why same theme doesn't apply to different views in Razor?

I have created a two view for displaying and editing using Razor in MVC.
When I run the program the page background image appears and works as I want, but when I click on the edit link it loads the edit view and it just shows a white background.
The first time I thought it doesn't apply the theme. Then I selected the edit view and run it again. There I could see the background image as I expected. But when I go to the displaying view, there wasn't any background image.
Why is this happening? Any suggestions?
These are two images relevant to displaying view and edit view.
This is the view of the page display page
#model OnlineElection.DAL.person
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<link rel="stylesheet" href="http://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.3/css/materialize.min.css">
<script type="text/javascript" scr="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.3/js/materialize.min.js"></script>
<div>
<h4>Profile</h4>
<hr />
<dl class="dl-horizontal" style="width:40%; margin-left:10%">
<dt>
#Html.DisplayNameFor(model => model.SID)
</dt>
<dd>
#Html.EditorFor(model => model.SID, new { htmlAttributes = new { #readonly = "" } })
</dd>
<dt>
#Html.DisplayNameFor(model => model.FirstName)
</dt>
<dd>
#Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { #readonly = "" } })
</dd>
<dt>
#Html.DisplayNameFor(model => model.LastName)
</dt>
<dd>
#Html.EditorFor(model => model.LastName, new { htmlAttributes = new { #readonly = "" } })
</dd>
<dt>
#Html.DisplayNameFor(model => model.DOB)
</dt>
<dd>
#Html.EditorFor(model => model.DOB, new { htmlAttributes = new { #readonly = "" } })
</dd>
<dt>
#Html.DisplayNameFor(model => model.email)
</dt>
<dd>
#Html.EditorFor(model => model.email, new { htmlAttributes = new { #readonly = "" } })
</dd>
<dt>
#Html.DisplayNameFor(model => model.password)
</dt>
<dd>
#Html.EditorFor(model => model.password, new { htmlAttributes = new { #readonly = "" } })
</dd>
</dl>
</div>
<p>
#Html.ActionLink("Edit", "Edit", new { id = Model.SID })
</p>
Code of edit view
#model OnlineElection.DAL.person
#{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Edit</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>person</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.Person_ID)
<div class="form-group">
#Html.LabelFor(model => model.Phone, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Phone, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Phone, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Address, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Address, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Address, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.FirstName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.FirstName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.LastName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.LastName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.LastName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.DOB, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.DOB, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.DOB, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.SID, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.SID, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.SID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.email, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.email, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.email, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.password, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.password, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.password, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.AdminApproved, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
#Html.EditorFor(model => model.AdminApproved)
#Html.ValidationMessageFor(model => model.AdminApproved, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}

Want to use same model twice on same view with different type?

Currently I am working with asp mvc view
I want to render data on one view...that have display data and create form on same view but it can not be done..because controller return IEnumerable type data and create form's editorfor does not contain definition for that.
I have one model "T4.Models.Order"
if i use this model as #model IEnumerable<T4.Models.Order>
create form razor syntax shows error
if i use this model as #model T4.Models.Order
display data show's error
Here is my code
#model IEnumerable<T4.Models.Order>
#{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.CustomerID)
</th>
<th>
#Html.DisplayNameFor(model => model.CustomerID)
</th>
<th>
#Html.DisplayNameFor(model => model.OrderDate)
</th>
<th>
#Html.DisplayNameFor(model => model.RequiredDate)
</th>
<th>
#Html.DisplayNameFor(model => model.ShippedDate)
</th>
<th>
#Html.DisplayNameFor(model => model.ShipVia)
</th>
<th>
#Html.DisplayNameFor(model => model.Freight)
</th>
<th>
#Html.DisplayNameFor(model => model.ShipName)
</th>
<th>
#Html.DisplayNameFor(model => model.ShipAddress)
</th>
<th>
#Html.DisplayNameFor(model => model.ShipCity)
</th>
<th>
#Html.DisplayNameFor(model => model.ShipRegion)
</th>
<th>
#Html.DisplayNameFor(model => model.ShipPostalCode)
</th>
<th>
#Html.DisplayNameFor(model => model.ShipCountry)
</th>
</tr>
</thead>
#*#{
IEnumerable<T4.Models.Order> m = #model;
}*#
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.CustomerID)
</td>
<td>
#Html.DisplayFor(modelItem => item.EmployeeID)
</td>
<td>
#Html.DisplayFor(modelItem => item.OrderDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.RequiredDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.ShippedDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.ShipVia)
</td>
<td>
#Html.DisplayFor(modelItem => item.Freight)
</td>
<td>
#Html.DisplayFor(modelItem => item.ShipName)
</td>
<td>
#Html.DisplayFor(modelItem => item.ShipAddress)
</td>
<td>
#Html.DisplayFor(modelItem => item.ShipCity)
</td>
<td>
#Html.DisplayFor(modelItem => item.ShipRegion)
</td>
<td>
#Html.DisplayFor(modelItem => item.ShipPostalCode)
</td>
<td>
#Html.DisplayFor(modelItem => item.ShipCountry)
</td>
</tr>
}
</table>
</body>
</html>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Order</h4>
<hr />
#Html.ValidationSummary(true)
<div class="form-group">
#Html.LabelFor(model => model.CustomerID, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.CustomerID)
#Html.ValidationMessageFor(model => model.CustomerID)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.EmployeeID, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.EmployeeID)
#Html.ValidationMessageFor(model => model.EmployeeID)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.OrderDate, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.OrderDate)
#Html.ValidationMessageFor(model => model.OrderDate)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.RequiredDate, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.RequiredDate)
#Html.ValidationMessageFor(model => model.RequiredDate)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ShippedDate, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ShippedDate)
#Html.ValidationMessageFor(model => model.ShippedDate)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ShipVia, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ShipVia)
#Html.ValidationMessageFor(model => model.ShipVia)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Freight, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Freight)
#Html.ValidationMessageFor(model => model.Freight)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ShipName, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ShipName)
#Html.ValidationMessageFor(model => model.ShipName)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ShipAddress, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ShipAddress)
#Html.ValidationMessageFor(model => model.ShipAddress)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ShipCity, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ShipCity)
#Html.ValidationMessageFor(model => model.ShipCity)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ShipRegion, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ShipRegion)
#Html.ValidationMessageFor(model => model.ShipRegion)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ShipPostalCode, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ShipPostalCode)
#Html.ValidationMessageFor(model => model.ShipPostalCode)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ShipCountry, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ShipCountry)
#Html.ValidationMessageFor(model => model.ShipCountry)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
Controller Code:
public ActionResult Index()
{
return View(db.Order.ToList());
}
Note : I don't want to change anything on controller side.
Just create the object of model class and use to render your form..
Here is the code
#{
T4.Models.Order o = new T4.Models.Order();
}
Your Form
#using (Html.BeginForm("Create","Orders"))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Order</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => o.CustomerID, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => o.CustomerID, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => o.CustomerID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => o.EmployeeID, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => o.EmployeeID, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => o.EmployeeID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => o.OrderDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => o.OrderDate, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => o.OrderDate, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => o.RequiredDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => o.RequiredDate, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => o.RequiredDate, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => o.ShippedDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => o.ShippedDate, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => o.ShippedDate, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => o.ShipVia, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => o.ShipVia, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => o.ShipVia, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => o.Freight, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => o.Freight, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => o.Freight, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => o.ShipName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => o.ShipName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => o.ShipName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => o.ShipAddress, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => o.ShipAddress, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => o.ShipAddress, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => o.ShipCity, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => o.ShipCity, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => o.ShipCity, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => o.ShipRegion, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => o.ShipRegion, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => o.ShipRegion, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => o.ShipPostalCode, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => o.ShipPostalCode, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => o.ShipPostalCode, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => o.ShipCountry, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => o.ShipCountry, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => o.ShipCountry, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
If you want to list the existing orders and show the create form in the same view, you should create a new view model which has properties for this. You can inherit this from your existing T4.Models.Order.
public class CreateAndListVm : T4.Models.Order
{
public List<Order> Orders { set;get;}
public CreateAndListVm()
{
this.Orders = new List<Order>();
}
}
Now your view will be strongly typed to this view model
#model CreateAndListVm
<h2>New item</h2>
#using(Html.BeginForm())
{
#Html.TextBoxFor(s=>s.CustomerID)
<input type="submit" />
}
<h3>Existing orders</h3>
#foreach(var item in Model.Orders)
{
<p>#item.CustomerID</p>
}
Ofcourse that means, you should send an object of CreateAndListVm from your action method.
public ActionResult Index()
{
var vm = new CreateAndListVm();
// to do : Load the orders to vm.Orders;
return View(vm)
}
If you do not want to change the controller and it is returning a collection of Order to the view, you have another alternative. Don't use the Html helper methods to generate the input form fields. Just write the html code for the input fields yourself.
Assuming your action method is returning a list of Order class
public ActionResult Index()
{
var orders = new List<Order>();
return View(orders);
}
Now in your view which is strongly typed to a collection of Order
#model IEnumerable<Order>
<h2>Create</h2>
<form action="Create" method="POST">
<input type="text" name="CustomerID" />
<input type="submit" />
</form>
<h3>Existing orders</h3>
#foreach(var item in Model)
{
<p>#item.CustomerID</p>
}

Resources