Cannot apply indexing with [] to an expression of type 'HttpRequest' - asp.net-mvc

I am trying to get a value from a textbox of my View.
This is my View:
#model MyDataIndexViewModel
#{
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<h1>Meine Daten</h1>
</div>
</div>
var item = Model.User;
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6 myDataTitle">Email</div>
<div class="col-xs-6 col-sm-6 col-md-6">
#Html.TextBox("txtEmail", "", new { placeholder = item.Email})
</div>
</div>
}
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<a class="btn btn-default pull-right" href="/ChangeMyData/Save">Speichern</a>
</div>
</div>
This is my Controller:
[HttpPost]
public ActionResult Save()
{
var email = Request["txtEmail"].ToString();
return View();
}
I get the error just as it says in the Title.
Thank you in advance!

VIEW:
#model MyDataIndexViewModel
#using (Html.BeginForm("Save", "CONTROLLER_NAME"))
{
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<h1>Meine Daten</h1>
</div>
</div>
var item = Model.User;
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6 myDataTitle">Email</div>
<div class="col-xs-6 col-sm-6 col-md-6">
#Html.TextBox("txtEmail", "", new { placeholder = item.Email, id="txtEmail"})
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<a class="submit btn btn-default pull-right">Speichern</a>
</div>
</div>
}
CONTROLLER
[HttpPost]
public ActionResult Save()
{
var email = Request.Form["txtEmail"].ToString();
return View();
}

You can either use strongly-typed viewmodel binding:
View
#model MyDataIndexViewModel
#* other stuff *#
#Html.TextBox("txtEmail", Model.Email)
Controller
[HttpPost]
public ActionResult Save(MyDataIndexViewModel model)
{
var email = model.Email;
return View();
}
Or use a TextBoxFor directly for model binding:
#model MyDataIndexViewModel
#* other stuff *#
#Html.TextBoxFor(model => model.Email)
Or if you still want to use HttpRequest members, Request.Form collection (a NameValueCollection) is available to retrieve text from txtEmail input:
[HttpPost]
public ActionResult Save()
{
var email = Request.Form["txtEmail"].ToString();
return View();
}
Note that Request["txtEmail"] is discouraged due to no compile time safety applied for it, because the key value may retrieved from Request.QueryString, Request.Form or other HttpRequestBase members.
Similar issue:
MVC TextBox with name specified not binding model on post
Access form data into controller using Request in ASP.NET MVC

Related

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1 .

Welcome,
I have several pictures, when I click on one it should take me to another window of this picture.
But this does not happen, but I get this window with this error message:
The model item passed into the dictionary is of type
'System.Collections.Generic.List`1[FirstProject.Models.student]', but
this dictionary requires a model item
I will put the codes related to the problem:
HomeController
using FirstProject.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace FirstProject.Controllers
{
public class HomeController : Controller
{
level_8_coursesEntities db = new level_8_coursesEntities();
public ActionResult Index()
{
List<level_8_courses> list = db.level_8_courses.ToList();
return View(list);
}
public ActionResult Numberofstudents()
{
List<level_8_courses> list = db.level_8_courses.ToList();
return View(list);
}
public ActionResult Students(int? id)
{
List<student> studentlist = null;
if (id.HasValue)
{
studentlist = db.students.Where(x => x.corId == id).ToList();
}
else
{
// Set the `studentlist` to some default value when `id` doesn't defined or not a number.
studentlist = null;
}
return View(studentlist);
}
Number of students.chtml
#{
ViewBag.Title = "Numberofstudents";
}
#using FirstProject.Models;
#model List<level_8_courses>
<div class="container">
<section class="page-section portfolio" id="portfolio">
<div class="container">
<!-- Portfolio Section Heading-->
<h2 class="page-section-heading text-center text-uppercase text-secondary mb-0">Numberofstudents</h2>
<!-- Icon Divider-->
<div class="divider-custom">
<div class="divider-custom-line"></div>
<div class="divider-custom-icon"><i class="fas fa-star"></i></div>
<div class="divider-custom-line"></div>
</div>
<!-- Portfolio Grid Items-->
<div class="row justify-content-center">
<!-- Portfolio Item 1 data-bs-target="#portfolioModal" -->
#foreach (var item in Model)
{
<div class="col-md-6 col-lg-4 mb-5">
<a href="/Home/students/#item.Id">
<div class="portfolio-item mx-auto" data-bs-toggle="modal">
<div class="portfolio-item-caption d-flex align-items-center justify-content-center h-100 w-100">
<div class="portfolio-item-caption-content text-center text-white"><i class="fas fa-plus fa-3x"></i></div>
</div>
<img class="img-fluid" style="height:100px;width:100px" src="#item.photo" alt="" />
</div>
</a>
<div style="text-align:center">
<h5>#item.corName</h5>
</div>
</div>
}
</div>
</div>
</section>
</div>
Students.chtml
#using FirstProject.Models;
#model List<student>
<div class="container">
<div class="row">
#foreach (var item in Model)
{
<div class="card col-sm-6">
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="..." alt="Card image cap">
<div class="card-body">
<h5 class="card-title">#item.sName</h5>
<p class="card-text">#item.sEmail</p>
</div>
</div>
</div>
}
</div>
</div>
Thanks
fix students action, if you don't have any students create an empty list as a model, and remove studentlist = null;
List<student> studentlist = null
if (id!=null && id > 0)
studentlist = db.students.Where(x => x.corId == id).ToList();
if(studentlist==null) studentslist= new List<student>();
return View(studentlist);

Calling a view from different models in ASP.NET MVC

In my ASP.NET MVC application in the view, I'm calling another view that is not related to the current model. There I need some help that how to call the different model views from another view.
#model Asp_PASMVC.Models.VehicleService
#using Asp_PASMVC.Infrastructure
#{
ViewBag.Title = "View";
Layout = "~/Views/Shared/_Layout.cshtml";
List<SelectListItem> CompanyList = (List<SelectListItem>)TempData.Peek("ComapnyList");
List<SelectListItem> ReqTypes = (List<SelectListItem>)TempData.Peek("RequestTyleList");
List<SelectListItem> Employees = (List<SelectListItem>)TempData.Peek("EmployeeList");
List<SelectListItem> Location = (List<SelectListItem>)TempData.Peek("LocationList");
Asp_PASMVC.Models.AppRequest RequestDetails = (Asp_PASMVC.Models.AppRequest)TempData.Peek("RequestDetails");
}
#{
Html.RenderPartial("_MainRequestView", RequestDetails);
}
#using (Html.BeginForm("WorkshopUpdate", "VehicleService", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
#Html.HiddenFor(model => model.Req_Id)
#Html.AntiForgeryToken()
if (Model != null && Model.VehicleServiceApproveDetails != null)
{
foreach (Asp_PASMVC.Models.VehicleServiceApproveDetails Emp in Model.VehicleServiceApproveDetails)
{
Html.RenderPartial("_WorkshopUpdate", Emp);
}
}
<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-12">
<!-- Default box -->
<div class="card">
<div class="card-header">
<h3 class="card-title">Approver Details</h3>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
<i class="fas fa-minus"></i>
</button>
</div>
</div>
<div class="card-body">
<div>
<fieldset id="pnlApproverList" style="display:none">
<legend><h5>To whom you want to send this request for approval ? </h5> </legend>
<br />
<ul id="RequApprover" style="list-style-type: none">
#if (Model != null && Model.ApprovalPartyList != null)
{
foreach (Asp_PASMVC.Models.ApprovalParty Emp in Model.ApprovalPartyList)
{
Html.RenderPartial("_ApprovalView", Emp);
}
}
</ul>
<button type="button" id="addAnotherApprover" class="btn btn-success" href="#" onclick="this.style.display = 'none';">Add</button>
<script type="text/javascript">
$(function () {
// $("#movieEditor").sortable();
$("#addAnotherApprover").click(function () {
$.get('/VehicleService/AddApproverToReq', function (template) {
$("#RequApprover").append(template);
});
});
});
</script>
<br />
</fieldset>
</div>
</div>
<!-- /.card-footer-->
</div>
<!-- /.card -->
</div>
</div>
</div>
</section>
<div class="card-footer">
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Update and Sent" class="btn btn-success" />
</div>
</div>
</div>
}
<p>
#Html.ActionLink("Back to List", "Index")
</p>
So likewise here the model is VehicleService. So within that view, I want to call another view that is not within the vehicleservice model.
But I cannot load that partial view within this view. Is there any way to do this?
#model Asp_PASMVC.Models.ApprovalParty
#using Asp_PASMVC.Infrastructure
#{
string UserLvel = TempData.Peek("UserLevelClaims").ToString();
}
<li style="padding-bottom:15px">
#using (Html.BeginCollectionItem("ApprovalPartyList"))
{
<div class="row">
<div class="col-md-5 col-sm-5">
<div class="form-group">
<label>
#Html.RadioButtonFor(m => m.Approve_Type, false)
<span class="radiomargin">For Manager</span>
</label>
<br />
#if (UserLvel != "1")
{
<label>
#Html.RadioButtonFor(m => m.Approve_Type, true)
<span class="radiomargin">For Top Manager </span>
</label>
#Html.ValidationMessageFor(model => model.Approve_Type, "", new { #class = "text-danger" })
}
</div>
</div>
</div>
<br />
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group row">
Select the Approver
<div class="col-sm-8">
#Html.DropDownListFor(model => model.Approver_Id, new List<SelectListItem>(), new { #id = "ddlEmployees", #class = "js-dropdown" })
#Html.ValidationMessageFor(model => model.Approver_Id, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
}
</li>
Create a ViewModel which can have both Properties and pass that viewmodel to View
Model class:
public class VehicleSerivceViewModel
{
public VehicleService VehicleService { get; set; }
public ApprovalParty ApprovalParty { get; set; }
}
In View :
#model Asp_PASMVC.Models.VehicleServiceVewModel
pass ViewModel to partial as below:
#Model.ApprovalParty

Html.CheckBoxFor Returning Incorrect Value If Originally Unchecked

I have a ViewModel containing information for an object called ContactEvent. ContactEvent has a boolean called IsActive. I'm using #Html.CheckBoxFor() for a checkbox to let the user alter its value. The problem is that if the value is originally true and the user sets it to false, the model passed through the POST has the updated value. But if the value is originally false, the model passed through the POST always returns false still. I have no idea what is causing it.
View
#model ContactEventViewModel
#{
ViewBag.Title = "Contact Event";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<head>
<script src="~/datatables/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="~/datatables/css/jquery.dataTables.min.css" />
</head>
<body>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
#using (Html.BeginForm("ContactEvent", "ContactEvents", FormMethod.Post))
{
<h1>Edit Contact Event</h1>
#Html.HiddenFor(Model => Model.ContactEvent.ContactEventSysID, new { #Value = Model.ContactEvent.ContactEventSysID })
<div class="form-group row">
<label>Customer</label>
<div class="col-md-7 col-lg-7">
<Label>#String.Format("{1}, {0}", Model.Customer.FirstName, Model.Customer.LastName)</Label>
#Html.HiddenFor(Model => Model.Customer.CustomerSysID, new { #Value = Model.Customer.CustomerSysID })
</div>
</div>
<div class="form-group row">
<label>Date</label>
<div class="col-md-7 col-lg-7">
#Html.EditorFor(model => model.ContactEvent.Date, new { htmlAttributes = new { #class = "datepicker", #Name = "date", #PlaceHolder = "mm/dd/yyyy" } })
</div>
</div>
<div class="form-group row">
<label>Event Type</label>
<div class="col-md-7 col-lg-7">
#Html.DropDownListFor(Model => Model.ContactEvent.ContactEventTypeSysID, new SelectList(Model.ListOfContactEventTypes, "ContactEventTypeSysID", "Description"))
</div>
</div>
<div class="form-group row">
<label>Outcome</label>
<div class="col-md-7 col-lg-7">
#Html.DropDownListFor(Model => Model.ContactEvent.OutcomeSysID, new SelectList(Model.ListOfOutcomes, "OutcomeSysID", "Description"))
</div>
</div>
<div class="form-group row">
<label>Note</label>
<div class="col-md-7 col-lg-7">
#Html.TextBoxFor(Model => Model.ContactEvent.Note, new { #Value = Model.ContactEvent.Note })
</div>
</div>
<div class="form-group row">
<label>Created Date</label>
<div class="col-md-7 col-lg-7">
<label>#Model.ContactEvent.CreatedDate</label>
</div>
</div>
<div class="form-group row">
<label>Last Updated Date</label>
<div class="col-md-7 col-lg-7">
<label>#Model.ContactEvent.LastUpdatedDate</label>
</div>
</div>
<div class="form-group row">
<label>Active?</label>
<div class="col-md-7 col-lg-7">
#Html.CheckBoxFor(Model => Model.ContactEvent.IsActive, new { #Value = Model.ContactEvent.IsActive })
</div>
</div>
<input type="submit" id="updateButton" value="Submit" />
}
</div>
</body>
Method in Controller
[HttpPost]
public IActionResult ContactEvent(ContactEventViewModel contactEventViewModel)
{
if (ModelState.IsValid)
{
string connectionString = Startup.ConnectionString;
string query = "UPDATE [CRM].[dbo].[ContactEvent] " +
"SET [Date]=#Date, [ContactEventTypeSysID]=#ContactEventTypeSysID, [OutcomeSysID]=#OutcomeSysID, [Note]=#Note, [LastUpdatedDate]=#LastUpdatedDate, [IsActive]=#IsActive " +
"WHERE [ContactEventSysID]=#ContactEventSysID";
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("#Date", contactEventViewModel.ContactEvent.Date);
command.Parameters.AddWithValue("#ContactEventTypeSysID", contactEventViewModel.ContactEvent.ContactEventTypeSysID);
command.Parameters.AddWithValue("#OutcomeSysID", contactEventViewModel.ContactEvent.OutcomeSysID);
if (contactEventViewModel.ContactEvent.Note == null)
{
command.Parameters.AddWithValue("#Note", DBNull.Value);
}
else
{
command.Parameters.AddWithValue("#Note", contactEventViewModel.ContactEvent.Note);
}
command.Parameters.AddWithValue("#LastUpdatedDate", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
command.Parameters.AddWithValue("#IsActive", contactEventViewModel.ContactEvent.IsActive);
command.Parameters.AddWithValue("#ContactEventSysID", contactEventViewModel.ContactEvent.ContactEventSysID);
connection.Open();
command.ExecuteNonQuery();
}
}
}
return RedirectToAction("ContactEvent", "ContactEvents", new { ContactEventSysID = contactEventViewModel.ContactEvent.ContactEventSysID });
}
Let me know if you need any other code. Thanks in advance.
Please remove #Value = Model.ContactEvent.IsActive. This will let your model properly handle the value. Do not set any values unless you are sure, that you want to override model behavior.

The Model Passed to Dictionary is of one type whereas the Dictionary Requires Model Item of Type ViewModels

I am working with the partial views to attain a functionality in which I have to delete data from two tables having a one to many relationship in database. For this, I am using same “Delete” function in controller for both tables. I have added two models for these tables in the Delete view using ViewModels and have rendered both partial views in the main Delete Page to display relevant data on each delete view but after doing all this coding I am getting the following error.
The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.tblPatientBill_804486DE0CB5E4B1C416CFC35E7B001C20B1FDB3674F40F1811012FFC9BAA908', but this dictionary requires a model item of type 'HMS.ViewModels.DeleteViewModel'.
Here is my Code Details:
PatientsBillController:
public ActionResult Delete(int? id,DeleteViewModel model)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
PatientsBillViewModel billmodell = new PatientsBillViewModel();
PatientsBillDetailViewModel billdetailmodell = new PatientsBillDetailViewModel();
tblPatientBill bill = db.tblPatientBills.Find(id);
tblPatientBillDetail billdetail = db.tblPatientBillDetails.Find(id);
if (billdetail == null)
{
if (bill == null)
{
return HttpNotFound();
}
else
{
billmodell.ID = bill.ID;
billmodell.PatientAppointmentID = bill.PatientAppointmentID;
billmodell.BillNo = bill.BillNo;
billmodell.Amount = bill.Amount;
billmodell.Discount = bill.Discount;
billmodell.CreatedAt = bill.CreatedAt;
billmodell.CreatedBy = bill.CreatedBy;
billmodell.Description = bill.Description;
return View(billmodell);
}
}
else
{
billdetailmodell.ID = billdetail.ID;
billdetailmodell.PatientBillID = billdetail.PatientBillID;
billdetailmodell.Amount = billdetail.Amount;
billdetailmodell.CreatedAt = billdetail.CreatedAt;
billdetailmodell.CreatedBy = billdetail.CreatedBy;
billdetailmodell.Description = billdetail.Description;
return View(billdetail);
}
}
Delete.cshtml:
#using HMS.ViewModels
#model HMS.ViewModels.DeleteViewModel
#{
ViewBag.Title = "Delete";
Layout = null;
}
#{Html.RenderPartial("PatientsBillDelete", Model); }
#{Html.RenderPartial("PatientsBillDetail", Model); }
ViewModel.cs:
public class DeleteViewModel
{
public PatientsBillViewModel billmodel { set; get; }
public PatientsBillDetailViewModel billdetailmodel { set; get; }
}
public class PatientsBillViewModel
{
public int ID { get; set; }
public int PatientAppointmentID { get; set; }
public string BillNo { get; set; }
public float Amount { get; set; }
public float Discount { get; set; }
public string CreatedAt { get; set; }
public string CreatedBy { get; set; }
public string Description { get; set; }
}
public class PatientsBillDetailViewModel
{
public int ID { get; set; }
public int PatientBillID { get; set; }
public float Amount { get; set; }
public string CreatedAt { get; set; }
public string CreatedBy { get; set; }
public string Description { get; set; }
}
PartialViews are:
PatientBillDelete.cshtml
PatientBillDetail.cshtml
PatientBillDelete.cshtml:
#using HMS.ViewModels
#model HMS.ViewModels.DeleteViewModel
#{
ViewBag.Title = "PatientsBill Delete";
}
<section class="content">
<div class="container-fluid">
<div class="block-header">
<h2>Delete</h2>
</div>
<div class="row clearfix">
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="card">
<h3>Are You Sure You Want To Delete This?</h3>
<div class="body">
<div class="row clearfix">
<div class="col-sm-6 col-md-6 col-lg-6">
<div class="form-group">
<div class="form-line">
#Html.DisplayNameFor(m => m.billmodel.ID)
<br />
#Html.DisplayFor(m => m.billmodel.ID)
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-sm-6 col-md-6 col-lg-6">
<div class="form-group">
<div class="form-line">
#Html.DisplayNameFor(m => m.billmodel.PatientAppointmentID)
<br />
#Html.DisplayFor(m => m.billmodel.PatientAppointmentID)
</div>
</div>
</div>
<div class="col-sm-6 col-md-6 col-lg-6">
<div class="form-group">
<div class="form-line">
#Html.DisplayNameFor(m => m.billmodel.BillNo)
<br />
#Html.DisplayFor(m => m.billmodel.BillNo)
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-sm-6 col-md-6 col-lg-6">
<div class="form-group">
<div class="form-line">
#Html.DisplayNameFor(m => m.billmodel.Amount)
<br />
#Html.DisplayFor(m => m.billmodel.Amount)
</div>
</div>
</div>
<div class="col-sm-6 col-md-6 col-lg-6">
<div class="form-group">
<div class="form-line">
#Html.DisplayNameFor(m => m.billmodel.Discount)
<br />
#Html.DisplayFor(m => m.billmodel.Discount)
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-sm-6 col-md-6 col-lg-6">
<div class="form-group">
<div class="form-line">
#Html.DisplayNameFor(m => m.billmodel.CreatedAt)
<br />
#Html.DisplayFor(m => m.billmodel.CreatedAt)
</div>
</div>
</div>
<div class="col-sm-6 col-md-6 col-lg-6">
<div class="form-group">
<div class="form-line">
#Html.DisplayNameFor(m => m.billmodel.CreatedBy)
<br />
#Html.DisplayFor(m => m.billmodel.CreatedBy)
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-sm-6 col-md-6 col-lg-6">
<div class="form-group">
<div class="form-line">
#Html.DisplayNameFor(m => m.billmodel.Description)
<br />
#Html.DisplayFor(m => m.billmodel.Description)
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-sm-6 col-md-6 col-lg-6">
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-actions no-color">
#Html.HiddenFor(m => m.billmodel.ID)
<input type="submit" value="Delete" class="btn btn-default" /> |
#Html.ActionLink("Back to List", "EditBill")
</div>
}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
PatientsBillDetail.cshtml
#using HMS.ViewModels
#model HMS.ViewModels.DeleteViewModel
#{
ViewBag.Title = "PatientsBill Delete";
}
<section class="content">
<div class="container-fluid">
<div class="block-header">
<h2>Delete</h2>
</div>
<div class="row clearfix">
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="card">
<h3>Are You Sure You Want To Delete This?</h3>
<div class="body">
<div class="row clearfix">
<div class="col-sm-6 col-md-6 col-lg-6">
<div class="form-group">
<div class="form-line">
#Html.DisplayNameFor(model => model.billdetailmodel.ID)
<br />
#Html.DisplayFor(model => model.billdetailmodel.ID)
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-sm-6 col-md-6 col-lg-6">
<div class="form-group">
<div class="form-line">
#Html.DisplayNameFor(model => model.billdetailmodel.PatientBillID)
<br />
#Html.DisplayFor(model => model.billdetailmodel.PatientBillID)
</div>
</div>
</div>
<div class="col-sm-6 col-md-6 col-lg-6">
<div class="form-group">
<div class="form-line">
#Html.DisplayNameFor(model => model.billdetailmodel.Amount)
<br />
#Html.DisplayFor(model => model.billdetailmodel.Amount)
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-sm-6 col-md-6 col-lg-6">
<div class="form-group">
<div class="form-line">
#Html.DisplayNameFor(model => model.billdetailmodel.CreatedAt)
<br />
#Html.DisplayFor(model => model.billdetailmodel.CreatedAt)
</div>
</div>
</div>
<div class="col-sm-6 col-md-6 col-lg-6">
<div class="form-group">
<div class="form-line">
#Html.DisplayNameFor(model => model.billdetailmodel.CreatedBy)
<br />
#Html.DisplayFor(model => model.billdetailmodel.CreatedBy)
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-sm-6 col-md-6 col-lg-6">
<div class="form-group">
<div class="form-line">
#Html.DisplayNameFor(model => model.billdetailmodel.Description)
<br />
#Html.DisplayFor(model => model.billdetailmodel.Description)
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-sm-6 col-md-6 col-lg-6">
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-actions no-color">
#Html.HiddenFor(model => model.billdetailmodel.ID)
<input type="submit" value="Delete" class="btn btn-default" /> |
#Html.ActionLink("Back to List", "EditBill")
</div>
}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
I think there is problem with my controller logic how can I look for different ids passed in the Delete function and check in two different table and pass result to viewmodel?
Please tell me what I am doing wrong in it. Thanks in advance
You are actually returning returning wrong object to your View. You should be passing the DeleteViewModel object back to your View something like :
DeleteViewModel deleteViewModel = new DeleteViewModel ();
deleteViewModel.billmodell = billmodell;
deleteViewModel.billdetailmodel = billdetailmodell;
and then pass it in your View() method call:
return View(deleteViewModel);
Your code after loading records from db should be like:
DeleteViewModel deleteViewModel = new DeleteViewModel();
deleteViewModel.billmodell = new PatientsBillViewModel(); ;
deleteViewModel.billdetailmodel = new PatientsBillDetailViewModel();
tblPatientBill bill = db.tblPatientBills.Find(id);
tblPatientBillDetail billdetail = db.tblPatientBillDetails.Find(id);
if (bill != null)
{
deleteViewModel.billmodell.ID = bill.ID;
deleteViewModel.billmodell.PatientAppointmentID = bill.PatientAppointmentID;
deleteViewModel.billmodell.BillNo = bill.BillNo;
deleteViewModel.billmodell.Amount = bill.Amount;
deleteViewModel.billmodell.Discount = bill.Discount;
deleteViewModel.billmodell.CreatedAt = bill.CreatedAt;
deleteViewModel.billmodell.CreatedBy = bill.CreatedBy;
deleteViewModel.billmodell.Description = bill.Description;
}
if(billdetail != null)
{
deleteViewModel.billdetailmodel.ID = billdetail.ID;
deleteViewModel.billdetailmodel.PatientBillID = billdetail.PatientBillID;
deleteViewModel.billdetailmodel.Amount = billdetail.Amount;
deleteViewModel.billdetailmodel.CreatedAt = billdetail.CreatedAt;
deleteViewModel.billdetailmodel.CreatedBy = billdetail.CreatedBy;
deleteViewModel.billdetailmodel.Description = billdetail.Description;
}
return View(deleteViewModel);

How to show your hidden partial view programmatically in MVC

I have a partial view _ABC.cshtml that is hidden initially.
<div id="overview" style="display:none">
<h1> Overview</h1>
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 leftlist">
</div>
<div class="col-xs-9 col-sm-9 col-md-9 col-lg-9 overview">
<h3 class="isppagetitle">#Resources.Overview</h3>
<div class="row col-xs-12 col-sm-12 col-md-12 col-lg-12 content">
<div class="row">
......
But now from other view I want it to be rendered .
public ActionResult Index()
{
return PartialView("_AMgmtPartial");
}
I did in _AMgmtPartial..
<div class="account-root">
#{
Html.RenderPartial("_CustList");
Html.RenderPartial("_ABC", new { style = "display:block" });
}
<input type="hidden" id="accountmgmturl" value='#Url.Action("", "AccountMgmt")' />
...............
...............
But this style="display:block;" did not work. What did I do wrong ?
Html.RenderPartial doesn't have a parameter that accepts HtmlAttributes. This is confirmed by looking at the documentation for it.
The parameter that you have populated would be accessible from within the Controller though:
public ActionResult Index(string style)
{
// style will equal "display:block"
return PartialView("_AMgmtPartial");
}
You could take this value and pass it to your partial view via the view bag or a view model.
public ActionResult Index(string style = "display: none")
{
// style will equal "display:block"
ViewBag.PartialStyle = style;
return PartialView("_AMgmtPartial");
}
Then this makes it accessible from within your view:
<div id="overview" style="#ViewBag.PartialStyle">
<h1> Overview</h1>
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 leftlist">
</div>
<div class="col-xs-9 col-sm-9 col-md-9 col-lg-9 overview">
<h3 class="isppagetitle">#Resources.Overview</h3>
<div class="row col-xs-12 col-sm-12 col-md-12 col-lg-12 content">
<div class="row">
......
To do this though would be a dirty hack. As #Stephen mentioned a more preferable solution would be to wrap your partial view render statement in a <div> and on that you should set your initial visibility.
<div style="display: block">
#Html.Partial("_ABC");
</div>

Resources