Values don't change after posting back to view? - asp.net-mvc-3

Edit: I've changed my question and code to clarify my question better
I've got this (strongly typed view) that does use the values provided in the controller for that specific model
I want to add something to a Model from another model, after posting back from my httpPost action nothing happens...
Thanks in advance!
--------------------------------------other code to clarify my question a bit more----
public class Address
{
public int Id { get; set;}
public String Name { get; set;}
}
public class OtherAddress
{
public int Id { get; set;}
public String Name { get; set;}
public String City { get; set;}
}
public class MasterModel
{
public Address Address { get; set;}
public List<OtherAddress> OtherAddressess { get; set;}
}
public ActionResult Create()
{
MasterModel Model = new MasterModel();
Model.Person = new Person();
Model.Address = new Address();
Model.OtherAdressess = new List<OtherAddress>();
DBContext _db = new DBContext();
Model.OtherAdressess = _db.OtherAddressess.Where(a=> a.City == "Amsterdam");
return View(Model);
}
in the view
#model Project.Models.MasterModel
List<SelectListItems> items = new List<SelectListItems>();
foreach(var a in Model.OtherAddressess)
{
SelectListItem item = new SelectListItem();
item.Value = a.Id.toString();
item.Text = a.Street;
}
#using (#Html.BeginForm())
{
<div>
<select name="otheraddress">
foreach(var i in Items)
{
<option value=#i.Value>#i.Text</option>
}
</select>
<input type="submit" name="select" value="Select Address"/>
</div>
<div>
#Html.EditorFor(model => Model.Address.Name)
<div>
<p>
<input type="submit" value="Submit"/>
</p>
}
in post
[HttpPost]
public ActionResult Create(MasterModel Model)
{
String otherAddressSelected = Request.Params["select"];
if(!String.IsNullOrEmpty(otherAddressSelected))
{
int id = int.Parse(Request.Params["otheraddress"]);
DBContext _db = new DBContext();
OtherAddress oa = _db.OtherAddress.Single(oa=> oa.Id == id);
Model.Address.Name = oa.Name;
return View(Model);
}
//other stuff here
}

If you want to change the value of your model in a [HttpPost] controller you have to remove the modelstate for the instance/attribute that you want to change. For example:
[HttpPost]
public ActionResult Index(SomeModel model)
{
ModelState.Remove("Name");
model.Name = "some new name";
return View(model);
}
Got the answer from this example

I would create action method called details that would accept person id as parameter:
public ActionResult Details(int id)
{
// Get person and display
}
Your Create action method is for creating Person type objects, and not displaying their details. So logically what you are doing doesn't seem right to me.
There should be action method to display view for creating a person and equivalent HTTP action method for persisting it into the database.
I would then re-direct to an action method for displaying Person type object information.
return RedirectToAction("Details", new { id = Person.Id });

The input helpers in asp.net mvc will use the post values if they can find any before looking at the model.
In this situation here I think the problem is that you are trying to do more then one thing in the Create POST action. A action (as with any method in the application) should only do one thing. In your case I would do something like this (if I understand the work flow correctly that is):
//Action: SelectAddress
public ActionResult SelectAddress() {
var addresses = _db.OtherAddressess.Where(a=> a.City == "Amsterdam");
return View(new SelectAddressViewModel(addresses));
}
//View SelectAddress
....
<ul>
#foreach(var address in Model.Addresses) {
<li>
<a href="#Url.Action("Create", "Product", new { addressId = address.Id })">
#Model.Name
</a>
</li>
}
</ul>
....
//Action Create
public ActionResult Create(int addressId) {
var address = _db.OtherAddress.Single(oa=> oa.Id == addressId);
var Model = new MasterModel();
Model.Person = new Person();
Model.Address = new Address {
Name = address.Name
}
return View(Model);
}

Related

How does selected dropdown value get saved from View Component to main model?

ASP.NET Core 5 MVC web app. The question is HOW it works, not why it doesn't. I don't understand the mechanism and so don't want to see it fail from some "happy-fingers" coding accident in the future...
I have a main model that the controller expects on create:
public class ProductCategory : BaseClass
{
public int ProductId { get; set; }
public virtual Product Product { get; set; }
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
}
(BaseClass just has your typical record keeping fields).
I have a model for the view components; I need two, one for each dropdown, so you can easily imagine the other, with names modified to protect the innocent...:
Category:
public class CategoryList
{
public CategoryList()
{
Categories = new List<Category>();
}
public int CategoryId { get; set; }
[DisplayName("Categories")]
public List<Category> Categories { get; set; }
}
The category view component:
public class CategoryDropdownViewComponent : ViewComponent
{
private readonly ApplicationDbContext _db;
public CategoryDropdownViewComponent(ApplicationDbContext context)
{
_db = context;
}
public async Task<IViewComponentResult> InvokeAsync()
{
var items = await GetCategoriesAsync();
var TheView = "Default";
var list = new CategoryList();
if (items.Count == 0)
{
TheView = "CategoryMaintenanceLink";
}
else
{
items.Insert(0, new Category() { Id = 0, Name = "-- Please select an option --" });
list.Categories = items;
}
return View(TheView, list);
}
private Task<List<Category>> GetCategoriesAsync()
{
return _db.Category.ToListAsync();
}
}
And the default view for category (I store this and above in ~\Shared\Components\CategoryDropdown\):
#model CoolestProjectNameEver.Models.CategoryList
<p>
#Html.DropDownListFor(model => model.CategoryId, new SelectList(Model.Categories, "Id", "Name"), new { #class = "form-control" })
</p>
So, in my controller, I kick off create:
public IActionResult Create()
{
return View();
}
And in the Create view, amongst other things, I fire up the view components:
<div class="form-group">
<label asp-for="ProductId" class="control-label"></label>
#await Component.InvokeAsync("ProductDropdown")
</div>
<div class="form-group">
<label asp-for="CategoryId" class="control-label"></label>
#await Component.InvokeAsync("CategoryDropdown")
</div>
All works and the dropdown lists are filled. I can select options for both. Now the unknown part.
On to the POST method for Create:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(ProductCategory productCategory)
{
try
{
if (ModelState.IsValid) <--- breakpoint
{
_context.Add(productCategory);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return View(productCategory);
}
The breakpoint will show the correct selected values for CategoryId and ProductId.
So the question is, did this work because of a name match in the VC model to main controller model, and it auto filled somehow?
1 if my ViewComponent model had, say SelectedValueId instead of CategoryId, then it would fail because of a mismatch?
2 How did the value from a separate model in an async ViewComponent get plugged into the main model on postback?
In fact,if you change your Create view code to:
<div class="form-group">
#await Component.InvokeAsync("ProductDropdown")
</div>
<div class="form-group">
#await Component.InvokeAsync("CategoryDropdown")
</div>
it will also successfully binding.
The model binding in asp.net core is matched according to the name. If the name matches, the corresponding attribute will be bound to the model.
In your ViewComponent, your code(model => model.CategoryId):
#Html.DropDownListFor(model => model.CategoryId, new SelectList(Model.Categories, "Id", "Name"), new { #class = "form-control" })
will be given name =CategoryId in the generated html code
Then the name CategoryId is also in your ProductCategory model, if their names match, the binding will be successful.

Dropdown list in MVC3

I have a form that wanna to select category and tag from drop down list and bind it to a post , this is my ViewModel:
public class PostViewModel
{
public IList<Category> Category { get; set; }
public IList<Tag> Tag { get; set; }
}
and this is my get action :
public ActionResult Add()
{
ViewBag.CategoryList = new SelectList(_categoryRepository.GetAllCategory());
ViewBag.TagList = new SelectList(_tagRepository.GetAllTag());
return View();
}
now how can I get the Id dropdownlst to send it to the Post action?? :
<div>
#Html.LabelFor(post => post.Category)
#Html.DropDownListFor ????
#Html.ValidationMessageFor(post => post.Category)
</div>
I tried this one it it didn't work
<div>
#Html.LabelFor(post => post.Category)
#Html.DropDownListFor(post => post.Category, ViewBag.CategoryList as SelectList, "--- Select Category ---")
#Html.ValidationMessageFor(post => post.Category)
</div>
please give me a solution about this ,thanks
Try to avoid dynamic stuff like ViewBag and ViewData. Use strongly typed views.
ViewModel is just a POCO class which we will use to transfer data between your view and the action method. It will be specific to the view.
You have a viewmodel but you are not using it properly. Add 2 more properties to your viewmodel for getting the selected item from the dropdown. Also i changed the name of your proprties to pluralized for (Categories ,Tags) because they are for storing a collection.
public class PostViewModel
{
public List<SelectListItem> Categories{ get; set; }
public List<SelectListItem> Tags { get; set; }
public int SelectedCategory { set;get;}
public int SelectedTag { set;get;}
}
Now in your GET Action method, create an object of your view model and set the collection properties and then send that object to your view using View method.
public ActionResult Add()
{
var vm=new PostViewModel();
vm.Categories= GetAllCategories();
vm.Tags= GetAllTags();
return View(vm);
}
Assuming GetAllCategories and GetAllTags are 2 methods which returns a collection of SelectListItem for categories and tags.
public List<SelectListItem> GetAllCategories()
{
List<SelectListItem> categoryList=new List<SelectListItem>();
categoryList.Add(new SelectListItem { Text = "Sample", Value = "1" });
// TO DO : Read from your dB and fill here instead of hardcoding
return categoryList;
}
and in your view,
#model PostViewModel
#using(Html.BeginForm())
{
#Html.DropDownListFor(x => x.SelectedCategory,
new SelectList(Model.Categories,"Value","Text"), "Select")
#Html.DropDownListFor(x => x.SelectedTag,
new SelectList(Model.Tags,"Value","Text"), "Select")
<input type="submit" value="save" />
}
And in your HttpPost action method, you can get the selected items in the 2 properties we added
[HttpPost]
public ActionResult Add(PostViewModel model)
{
if(ModelState.IsValid)
{
//check model.SelectedCategory and model.SelectedTag
//save and redirect
}
//to do :reload the dropdown again.
return View(model);
}
you were close:
public class PostViewModel
{
public int CategoryId { get; set; } // <-- Altered
public int TagId { get; set; } // <-- Altered
}
<div>
#Html.LabelFor(post => post.Category)
#Html.DropDownListFor(post => post.CategoryId,
ViewBag.CategoryList as IEnumerable<SelectListItem>,
"--- Select Category ---")
#Html.ValidationMessageFor(post => post.Category)
</div>

MVC 3. Drop Down box

Hi I am new to MVC 3 just a beginner. I am trying to create a new drop down box in the view but I am getting the error saying "'System.Web.Mvc.HtmlHelper' does not contain a definition for 'DropDownListFor' and the best extension method overload 'System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>, System.Collections.Generic.IEnumerable)' has some invalid arguments".
Here is the View Code
<tr>
<td>
<label>
Customer Name
</label>
</td>
<td>
#Html.DropDownListFor(A => A.Roles, Model.Roles);
</td>
</tr>
Controller Code
public ActionResult Index()
{
var Model = new Customer();
Model.Roles = getRoles();
return View(Model);
}
private List<string> getRoles()
{
List<string> roles = new List<string>
{
"Developer",
"Tester",
"Project Manager",
"Team Lead",
"QA"
};
return roles;
}
Firdt I'd recommend that you create a viewmodel class for your view:
public class IndexViewModel
{
public IList<string> Roles { get; set; }
public string SelectedRole { get; set; }
}
Then call the view like this:
public ActionResult Index()
{
List<string> roles = new List<string>
{
"Developer",
"Tester",
"Project Manager",
"Team Lead",
"QA"
};
var viewModel = new IndexViewModel();
viewModel.Roles = roles;
return this.View(viewModel);
}
Then finally, render the dropdown list:
#model Mvc4.Controllers.IndexViewModel
#Html.DropDownListFor(model => model.SelectedRole, new SelectList(Model.Roles))
You needed a variable for storing the selected item (SelectedRole) and you need to wrap the selection of roles into a SelectList, since the dropdown helper cannot use an IEnumerable for the second parameter.

Binding a Generic List to a Dropdownlistfor in MVC3

I have a generic list method that returns a CategoryID and CategoryName.
I have spent enough time researching and cant seem to put it together. I very new at MVC.
Here is my DropdownList Method in a repository. I get back the data... So far so good.
public List<DropdownList> GetDDl()
{
return catDDL;
}
Here is my CONTROLLER CODE(attempt at it)
IEnumerable<SelectListItem> liCat =
userRepository.Getddl().Select(c => new SelectListItem
{
Value = c.DropDownID.ToString(),
Text = c.DropDownText
}
ViewBag.catItems = new SelecList(liCat,"Value","Text");
Here is my VIEW
#Html.Dropdownlist("catItems","Select Category)
Try to avoid dynamic stuff like ViewBag and ViewData. Use strongly typed views.
ViewModel is just a POCO class which we will use to transfer data between your view and the action method. It will be specific to the view.
ex : if you want to create a view which creates a product. So create a viewmodel like this
public class Product
{
public string Name { set;get;}
public IEnumerable<SelectListItem> Categories{ get; set; }
public string SelectedCategoryId { get; set; }
//Other Properties as needed
}
now in your GET action method, you create an object of this view model and initialize the values and send to the view.
public ActionResult Create()
{
var vm=new Product();
vm.Categories=userRepository.Getddl().
Select(c => new SelectListItem
{
Value = c.DropDownID.ToString(),
Text = c.DropDownText
});
return View(vm);
}
Now make your view strongly typed to our Product class and use the Html.DropDownListFor helper method.
#model PersonsProduct
#using (Html.BeginForm())
{
#Html.DropDownListFor(x => x.SelectedCategoryId,
new SelectList(Model.Categories,"Value","Text"), "Select")
<input type="submit" value="save" />
}
Now in your HttpPost , you can get the form values like this
[HttpPost]
public ActionResult Create(Product model)
{
if(ModelState.IsValid)
{
//check model.SelectedCategoryId
//save and redirect
}
//to do :reload the dropdown again.
return view(model);
}
Should just be:
Controller:
IEnumerable<SelectListItem> liCat = userRepository.Getddl().Select(c => new SelectListItem
{
Value = c.DropDownID.ToString(),
Text = c.DropDownText
}
ViewBag.catItems = liCat
View:
#Html.Dropdownlist("catItems", ViewBag.catItems)

DropDownListFor why it gets the Key and Nothing else

I have a property in my model very simple one:
Now this dropDown doesn't work right
#Html.DropDownListFor(m => m.Camp, new SelectList(ViewBag.Camps, "Id", "Name"))
it returns null instead of a chosen Camp, but if I change that into:
#Html.DropDownListFor(m => m.Camp.Id, new SelectList(ViewBag.Camps, "Id", "Name"))
It would return me a Camp object with correct Id, but the Name would be still null.
Why?
UPD:
And now another problem is if I choose the second approach it would screw up with unobtrusive validation. Although I'll be able to get the right camp based on the chosen id.
That's normal. Only the Id is posted to the controller action. That's how dropdown inside forms work. So that's all you can hope to get there. You will then use this Id to get the corresponding Camp object from the database:
[HttpPost]
public ActionResult Foo([Bind(Prefix = "Camp")]int id)
{
Camp camp = Repository.GetCamp(id);
...
}
Also please get rid of this ViewBag and use a real view model:
public class CampViewModel
{
public int Id { get; set; }
public IEnumerable<SelectListItem> Camps { get; set; }
}
and in the controller:
public ActionResult Index()
{
var model = new CampViewModel
{
Camps = Repository.GetCamps().Select(x => new SelectListItem
{
Value = x.Id.ToString(),
Text = x.Name
})
};
return View(model);
}
[HttpPost]
public ActionResult Index(int id)
{
Camp camp = Repository.GetCamp(id);
...
}
and the view:
#model CampViewModel
#using (Html.BeginForm())
{
#Html.DropDownListFor(
x => x.Id,
new SelectList(Model.Camps, "Value", "Text")
)
<input type="submit" value="OK" />
}

Resources