I am trying to pass a Model from my view to my controller using dropDownListFor.
After choosing something from the list it sends the model to my controller but it's content NULL.
This is what i have for the Model
public class Model
{
public int ModelId { get; set; }
[Required]
public string Name { get; set; }
}
This is what my ViewModel looks like
public class ModelVM
{
public List<Model> Models;
public Model SelectModel { get; set; }
public IEnumerable<SelectListItem> ModelItems
{
get { return new SelectList(Models, "ModelId", "Name"); }
}
}
The controller when i put data in the ViewModel looks like this
public ActionResult Index()
{
ModelVM modelVM= new ModelVM()
{
Models = manager.GetAllModels().ToList()
};
return View(modelVM);
}
Finally this is what i have in the View for the dropDownList
#using (Html.BeginForm("Home", "Home", FormMethod.Post))
{
#Html.DropDownListFor(model => model.SelectModel, Model.ModelItems)
<input type="submit" value="Go" />
}
So this is supposed to send Model to my controller.
But when i check the content of the passed Model in the controller, everything is NULL which isn't supposed to be because when i debug the view and check for the content of ModelItems, everything in the ModelItems is there.
Here is when i check the content of the passed Model
public ActionResult Home(Model model) <<<<<<<<<< Content == NULL
{
return View();
}
A <select> element on posts back a single value. Html has no concept of what your c# Model class is so you cannot bind to a complex object. You need to bind to the ModelId property of Model. Your view model should be
public class ModelVM
{
[Display(Name = "Model")]
public int SelectedModel { get; set; }
public SelectList ModelItems { get; set ;}
}
and in the controller
public ActionResult Index()
{
ModelVM modelVM = new ModelVM()
{
ModelItems = new SelectList(manager.GetAllModels(), "ModelId", "Name")
// SelectedModel = ? if you want to preselect an item
};
return View(modelVM);
}
and in the view
#Html.LabelFor(m => m.SelectedModel)
#Html.DropDownListFor(m => m.SelectedModel, Model.ModelItems)
and in the post method your model will be correctly bound with the ID of the selected model
Trying passing in type ModelVM to the Home action as that is the type being passed to Index view.
Instead of model.SelectModel in the drop-down declaration, you only need an int ID to capture which ID is selected.
Related
i have two class as below
Model:-
public class RegisterViewModel
{
public string Email { get; set; }
public AddressPropertyVM AddressProperty { get; set; }
}
public class AddressPropertyVM
{
public string StreetNo { get; set; }
}
Main Form
#model Application.Models.RegisterViewModel
{
#Html.TextBoxFor(m => m.Email)
#Html.TextBoxFor(m => m.FirstName)
#Html.Partial("_AddressPropertyPartial",Model.AddressProperty)
<button type="submit">Register</button>
}
Partial View Form
#model Application.Models.AddressPropertyVM
{
#Html.TextBoxFor(m => m.StreetNo)
}
I am creating asp.net mvc application.
I have create a partial view for AddressPropertyVM.
but we i post form(main form) at that time data of AddressProperty is null.
As per my understanding you want to use one partial view with more than one pages I mean multiple usage of one page in many pages.
Read this article and understand how it works.
Change your code as per article like below.
#Html.Partial("_AddressPropertyPartial",Model.AddressProperty)
To
#Html.Partial("_AddressPropertyPartial", Model.AddressProperty, new ViewDataDictionary() { TemplateInfo = new TemplateInfo() { HtmlFieldPrefix = "AddressProperty" } }
i have the following database table for the Compounds table (chemical compounds/elements in the periodic table) there are typos in table data so ignore them
the data is :
the controller :
public class CheckboxController : Controller
{
//
// GET: /Checkbox/
testdbEntities db = new testdbEntities();
[HttpGet]
public ActionResult Index()
{
var comps = db.Compounds.Select(c => new CompoundModel { Id=c.Id, CompoundName=c.Name, IsSelected=c.IsSelected}).ToList();
CheckboxVM cvm = new CheckboxVM { checkboxData=comps};
return View(cvm);
}
[HttpPost]
public string Index(IEnumerable<CheckboxVM> collection)
{
return "";
}
}
Model class CompoundModel is:
public class CompoundModel
{
public int Id { get; set; }
public string Code { get; set; }
public string CompoundName { get; set; }
public bool IsSelected { get; set; }
}
and the ViewModel CheckBoxVM:
public class CheckboxVM
{
public string Id { get; set; }
public string CompoundNmae { get; set; }
public bool IsSelected { get; set; }
public IEnumerable<CompoundModel> checkboxData { get; set; }
}
When the page loads it should display check boxes with names and if db table has checked on them (IsSelected=1) then they should be checked.In the post back i need to receive the id, of the user checked checkboxes. At the moment my code does meet the first requirement to check the checked checkboxes based on IsSelected on page load. Is there a way to fix this?
If you need a video with debugging please ask i will be happy to post : )
THE VIEW: (UPDATE)
#model recitejs1.Models.CheckboxVM
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
#using (Html.BeginForm())
{
foreach (var item in Model.checkboxData)
{
#Html.CheckBoxFor(x=>x.IsSelected, (item.IsSelected)?new{#checked="check"}:null)#item.CompoundName
#Html.HiddenFor(x=>x.Id, item.Id)
#Html.HiddenFor(x=>x.CompoundNmae, item.CompoundName)
}
<br><br>
<input type="submit" name="name" value="Send" />
}
You cannot use a foreach loop to generate form controls. It generates duplicate name attributes (that have no relationship to your model) and duplicate id attributes (invalid html).
Create a custom `EditorTemplate for your model
In /Views/Shared/EditorTemplates/CompoundModel.cshtml
#model recitejs1.Models.CompoundModel
#Html.HiddenFor(m => m.Id)
#Html.HiddenFor(m => m.CompoundName)
#Html.CheckBoxFor(m => m.IsSelected)
#Html.LabelFor(m => m.CompoundName)
Then in the main view
#model recitejs1.Models.CheckboxVM
....
#using (Html.BeginForm())
{
#Html.EditorFor(m => m.checkboxData)
<input type="submit" name="name" value="Send" />
}
The EditorFor() method will generate the correct html for each item in your collection
Note: You should inspect the html before and after you make this change to better understand how model binding works.
Note also that your POST method parameter needs to be
public string Index(CheckboxVM model)
since that's what the view is based on. However the only property of CheckboxVM that you use in the view is IEnumerable<CompoundModel> checkboxData in which case your view should be
#model IEnumerable<CompoundModel>
...
#Html.EditorFor(m => m)
and keep the POST method as it is (but change the GET method)
Let's say I created a model with a property as int? type.
public class MyModel
{
[Range(-100, 100, ErrorMessage="Too large")]
public int? MyValue { get; set; }
}
In the view I use it for a text box in the form to post back to server.
#using (Html.BeginForm())
{
<p>Enter some value</p>
<div>#Html.TextBoxFor(m => m.MyValue)</div>
<div>#Html.ValidationMessageFor(m => m.MyValue)</div>
<input type="submit" value="Submit" />
}
Here is the controller code
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new MyModel();
return View(model);
}
[HttpPost]
public ActionResult Index(MyModel model)
{
return View(model);
}
}
Now if I enter an extremely large number in the text box, e.g 111111111111111111111111111111111111111111111111111111111111111111111111111111, which is for sure too large for an integer data type. On post back, got message The value '111111111111111111111111111111111111111111111111111111111111111111111111111111' is invalid.
But I have no way to use the error message (localization and so on) I defined in my model.
using System.ComponentModel.DataAnnotations;
public class MyModel
{
[Range(int.MinValue, int.MaxValue)]
public int? MyValue {get; set; }
}
That should do the trick.
I have view and want to display content from the object stored in session one by one when user clicks on NEXT button. View uses WorldModel object to display the data. Id is hidden field and Content should be displayed on page.
View:
#model MvcApplication4.Models.WorldModel
#{
ViewBag.Title = "View1";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>View1</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
#Html.ValidationSummary()
<fieldset>
#Html.HiddenFor(model => model.Id)
#Html.DisplayFor(model => model.Content)
<input type="submit" value="Next" />
</fieldset>
}
Model: This class contains two properties id and content. Basically id is mapped to candidate id and Content is mapped to candidate name.
namespace MvcApplication4.Models
{
public class WorldModel
{
public int Id { get; set; }
public string Content { get; set; }
}
}
Controller: Class has two action methods one doesn’t take any parameter and other takes WorldModel as parameter.
namespace MvcApplication4.Controllers
{
public class WorldController : Controller
{
//
// GET: /World/
public ActionResult View1()
{
List<Candidate> obj = new List<Candidate>();
obj.Add(new Candidate() { Id = 1, Name = "ABCD", IsNameDispalyed = false});
obj.Add(new Candidate() { Id = 2, Name = "PQR", IsNameDispalyed = false });
obj.Add(new Candidate() { Id = 3, Name = "XYZ", IsNameDispalyed = false });
CandidateSession cs = new CandidateSession(){Candidates=obj};
Session["Can"] = cs;
return View();
}
[HttpPost]
public ActionResult View1(WorldModel worldModel)
{
CandidateSession cs = (CandidateSession)Session["Can"];
var can1 = cs.Candidates.Where(x => x.IsNameDispalyed == false).First();
can1.IsNameDispalyed = true;
return View(new WorldModel() {Id=can1.Id, Content=can1.Name });
}
}
public class Candidate
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsNameDispalyed { get; set; }
}
public class CandidateSession
{
public List<Candidate> Candidates { get; set; }
}
}
When I access URL as “../World/View1” it fills data to session object and click on NEXT button calls the action public ActionResult View1(WorldModel worldModel). In the method I get the object whose info is not displayed return the view with WorldModel object with id and Content. Now first time the Content is displayed but when the I click on NEXT button it call action public ActionResult View1(WorldModel worldModel). But the id and Content values of worldModel object are null. Why data is null, I set the values in the previous call?
I am trying to find the best way to use MVC for models which are only partially edited.
Below is an simple example.
Model
using System.ComponentModel.DataAnnotations;
public class SimpleModel
{
public int Id { get; set; }
public string Parent { get; set; }
[Required]
public string Name { get; set; }
}
View
using System.ComponentModel.DataAnnotations;
public class SimpleModel
{
public int Id { get; set; }
public string Parent { get; set; }
[Required]
public string Name { get; set; }
}
Controller
using System.Web.Mvc;
public class SimpleController : Controller
{
public ActionResult Edit(int id)
{ return View(Get(id)); }
[HttpPost]
public ActionResult Edit(int id, SimpleModel model)
{
if (model.Name.StartsWith("Child")) //Some test that is not done client-side.
{
Save(model);
//Get the saved data freshly.
//model = Get(id);
}
else
{
ModelState.AddModelError("", "Name should start with 'Child'");
}
//Is this the way to set the Parent property?
//var savedModel = Get(id);
//model.Parent = savedModel.Parent;
return View(model);
}
//Mock a database.
SimpleModel savedModel;
private void Save(SimpleModel model)
{ savedModel = new SimpleModel() { Id = model.Id, Name = model.Name }; }
private SimpleModel Get(int id)
{
if (savedModel == null)
{ return new SimpleModel() { Id = id, Parent = "Father", Name = "Child " + id.ToString() }; }
else
{ return new SimpleModel() { Id = savedModel.Id, Parent = "Father", Name = savedModel.Name }; }
}
}
The Name field is editable. The Parent field is only for reference and should not be updated. Therefore, it is rendered using DisplayFor.
Upon post, I receive a model with property Parent set to null. That's no problem as it will not be saved. However, when I simply return the received model to the view, the Parent field will no longer be displayed. When the model is valid, I can easily get it again from the database and thus get the Parent field's value back.
When the model is not valid, I would like to allow the user to correct input and attempt to save once more. There, I the received model's values that are input should be used, but the displayed values should be shown as well.
In reality, there are many more fields to be shown for reference, most often from different database entities than the one that is being edited.
I have seen suggestions to pass the fields as hidden fields in the view, but I feel very reluctant to read data from the client that should not be updated.
Is there a more elegant way to do this than copying these values into the model manually or passing them as hidden fields?
What about giving those un-editable properties to another model and let it take care of those properties?
View Model
public class PersonViewModel
{
public int Id { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
public PersonDetailsModel DetailsModel { get; set; }
}
Details Model
public class PersonDetailsModel
{
public string Mother { get; set; }
public string Father { get; set; }
public PersonDetailsModel() { }
public PersonDetailsModel(int personId)
{
// pull required model data from databases
var db = DBParentContext;
Mother = db.Parent.Where(m => m.ChildId == personId)
Father = db.Parent.Where(m => m.ChildId == personId)
}
}
Controller
public class PersonController : Controller
{
[HttpPost]
public ActionResult Edit(PersonViewModel viewModel)
{
viewModel.DetailsModel = new PersonDetailsModel(viewModel.Id)
if (ModelState.IsValid) {
// ~
}
return View(viewModel)
}
}
View
#model PersonViewModel
#Html.DisplayFor(m => m.DetailsModel.Mother)
#Html.DisplayFor(m => m.DetailsModel.Father)
#Html.TextBoxFor(m => m.FirstName)
#Html.TextBoxFor(m => m.LastName)
Since details like your Mother are un-editable then they're not really part of the "Edit" model, so I'd box like that away and try to let something else take care of them.
If you aren't going to update the Parent field, then it really doesn't matter if it's a hidden or not, since you won't update it on post.
I would use the hidden in this case, just make sure not to update that field.