in HomeController.cs
[HttpPost]
public ActionResult Index(SecurityMappingModel objModel)
{
//edit code
if (objModel.Id != Guid.Empty)
{
if (ModelState.IsValid)
{
mappings = (List<SecurityMappingModel>)Session["Mappings"];
var objRemove = mappings.FirstOrDefault(m => m.Id == objModel.Id);
if (objRemove != null)
{
mappings.Remove(objRemove);
mappings.Add(objModel);
ViewBag.Mappings = mappings;
}
Session["Mappings"] = mappings;
return RedirectToAction("Index");
}
inside objModel one of the field is having value 0 as it is a textfield i'm putting value into it but inside Controller oject objModel showing that field value is zero :( :( why???
if (yourGuid != Guid.Empty)
I also encountered this kind of error.
Maybe the solution to your problem is placing a code like this in your Home.cshtml view:
#Html.HiddenFor(model => model.Id)
Related
I am getting the value from dropdown to controller but when it goes back to View it returns Null
SCREENSHOT OF VIEW:-
SCREENSHOT OF CONTROLLER:-
public ActionResult Index(int departmentID)
{
ViewBag.dropdowndetail = null;
var strDDLValue = departmentID;
if (strDDLValue == null)
{
return HttpNotFound();
}
else
{
var emp = db.employees.ToList().FirstOrDefault(x=>x.depId==departmentID);
return View(emp);
}
}
ERROR:
I get it right
My viewbag was not getting any value in Post method so that why in View it was getting null Exception
I call my viewbag in Post method and give it the value.
change the following code with this.
public ActionResult Index(int departmentID)
{
var strDDLValue = departmentID;
if (strDDLValue == null)
{
return HttpNotFound();
}
else
{
var emp = db.employees.Where(x=>x.depId==departmentID).ToList();
ViewBag.dropdowndetail = db.departments.Distinct().ToList();
return View(emp);
}
}
You can use keyvalue approach in controller
ViewBag.dropdowndetail = db.departments.Distinct().Select(x => new KeyValuePair<int, string>(x.depid, x.DepartmentName));
and in View use ViewBag like this
#Html.DropDownListFor(model => model.depid, new SelectList((IEnumerable<KeyValuePair<int, string>>)ViewBag.dropdowndetail , "key", "value"), "--Select Departments--")
This is my controller code:
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult CheckBuildingName()
{
var isUnique = true;
string _buildingName = Request.Form["buildingName"]; // put your control name here
var connectionstring = ConnectionProvider();
AddBuildingModel model = new AddBuildingModel();
using (var context = new Notifier.AccountDatabase(connectionstring))
{
var objBuilding = (from building in context.Buildings
where building.buildingName == model.buildingName && building.buildingActive == true
select building).FirstOrDefault();
if (objBuilding == null)
{
isUnique = true;
}
else
{
isUnique = false;
}
}
if (isUnique == false)
{
return Json("Building already taken, Pleaes try with different name.", JsonRequestBehavior.AllowGet);
}
else
{
return Json(true, JsonRequestBehavior.AllowGet);
}
}
}
and my model is like below:
[System.ComponentModel.DisplayName("buildingName")]
[Remote("CheckBuildingName", "ConfigLocationController",ErrorMessage = "Building already exists!")]
public string buildingName { get; set; }
I am getting errors on this. The controller path cannot be found out or does not implement IController. What does that mean. Am I missing something ? Or is my code completely wrong. ? Please help
The reason for the error is that your RemoteAttribute is calling the CheckBuildingName method of ConfigLocationControllerController. Assuming that you controller is actually named ConfigLocationController, then you attributes need to be
[Display(Name = "Building Name")] // use this attribute, not DisplayName
[Remote("CheckBuildingName", "ConfigLocation",ErrorMessage = "Building already exists!")]
public string buildingName { get; set; }
However your method also contains errors. You initialize a new instance of a model and then use the value of its buildingName property (which will be null) in your query so it will always return null. In additional, you should add a parameter for the value your ajax call is submitting rather than using Request.Form. You method can be simply
[HttpPost]
public JsonResult CheckBuildingName(string buildingName)
{
bool exists = context.Buildings.Any(x => x.buildingName == buildingName && x.buildingActive);
return Json(!exists, JsonRequestBehavior.AllowGet);
}
which will return true if there is no match, or false if there is, in which case the message you have defined in the attribute will be displayed in the view assuming you have included #Html.ValidationMessageFor(m => m.buildingName)
net MVC. I have assigned a list of objects to the viewbag property can anyone please tell me as to how i can get the list in the view to use it in the drop down list? here is my controller code & view code
Controller:
public ActionResult GetSection(int sectionId,int contactId)
{
ContactDetailSectionModel contactDetailSection = new ContactDetailSectionModel { SectionId = sectionId,ContactId=contactId };
contactDetailSection.FetchAllSubsections();
ContactDetailSectionModel customSections = new ContactDetailSectionModel();
customSections.FetchCustomSubSections();
if(customSections != null && customSections.ContactDetailSubSections != null)
{
ViewBag.CustomSubSections = customSections.ContactDetailSubSections;
}
return PartialView("~/Views/Contacts/Details/EditSection.cshtml", contactDetailSection);
}
View Code:
#Html.DropDownListFor(m => m.ContactDetailSubSections[1], new SelectList(ViewBag.CustomSubSections , "Name", "Name",Model.ContactDetailSubSections[1].Name))
#Html.TextAreaFor(m => m.ContactDetailSubSections[1].Text)
I think the first parameter in your #Html.DropDownlist should be string or some scalar quantity it cannot be a collection.
I am new to asp and I would like to ask you for some help. I built store with MvcMusicStore tutorial help.Now I want to search in the database by using OrderId.As soon as the orderid is and if we click on the submit button it should display the corresponding rows from the database. My method is as follows
public ActionResult SearchOrder(int? myid)
{
var s = from sp in db.Railways
select sp;
string oid = myid.ToString();
if (!string.IsNullOrEmpty(oid))
{
s = s.Where(st => st.OrderID == (Convert.ToInt16(oid)));
}
return View(s.ToList());
}
Also i tired with the code as
public ActionResult SearchOrder(int? myid)
{
if (id != null)
{
if (ViewBag.OrderID == id.Value)
{
s = s.Where(st => st.OrderID == id);
}
}
return View(s);
}
In the second method when i tried it is neither displaying the contents nor showing the error.
Pls do help me.
Try this:
public ActionResult SearchOrder(int? myid)
{
var s = from sp in db.Railways
select sp;
if (myid.HasValue)
{
s = s.Where(st => st.OrderID == myid.Value);
}
return View(s.ToArray());
}
First your example will not work because string oid = myid.ToString(); will not be null or empty string if myid is null; Second example will fail (ViewBag.OrderID == id.Value) condition and moreover will not compile.
[HttpPost]
public ActionResult Index(SecurityMappingModel objModel)
{
//edit code
if (objModel.Id != Guid.Empty)
{
if (ModelState.IsValid)
{
mappings = (List<SecurityMappingModel>)Session["Mappings"];
var objRemove = mappings.FirstOrDefault(m => m.Id == objModel.Id);
if (objRemove != null)
{
mappings.Remove(objRemove);
mappings.Add(objModel);
ViewBag.Mappings = mappings;
}
Session["Mappings"] = mappings;
return RedirectToAction("Index");
}
inside objModel one of the field is having value 0 as it is a textfield i'm putting value into it but inside Controller oject objModel showing that field value is zero :( :( why???
Apparently one of your model values is not valid. You should be able to find out which one by looking at the items in ModelState.Values. Each item has an Errors property.
The result of this line should contain at least one item:
var errors = ModelState.Values.Where(value=>value.Errors.Any());