dropdown list sends null value to post action - asp.net-mvc-3

this is my Get actionresult :
public ActionResult Add()
{
ViewData["categoryList"]= _categoryRepository.GetAllCategory().
ToSelectList(c => c.Id, c => c.Name);
return View("Add");
}
this my razor that render the categoryList , and I have no trouble with that !
<div>
#Html.LabelFor(b => b.Category)
#Html.DropDownList("Category", ViewData["categoryList"] as IEnumerable<SelectListItem>)
#Html.ValidationMessageFor(b => b.Category)
</div>
finally after submitting the page , category select send via null value to post this action
[HttpPost]
public ActionResult Add(BlogPost blogPost)
{
if (ModelState.IsValid)
{
blogPost.PublishDate = DateTime.Now;
_blogPostRepository.AddPost(blogPost);
_blogPostRepository.Save();
return RedirectToAction("Add");
}
return new HttpNotFoundResult("An Error Accoured while requesting your order!");
}
could anybody tell me why ??

controller
public ActionResult Add()
{
ViewBag.CategoryList = new SelectList(_categoryRepository.GetAllCategory(), "Id", "Name");
// you dont need the specify View name
// like this: return View("Add")
// you need to pass your model.
return View(new BlogPost());
}
view
#Html.DropDownListFor(model => model.CategoryId, ViewBag.CategoryList as SelectList, "--- Select Category ---", new { #class = "some_class" })
controller post action
[HttpPost]
public ActionResult Add(BlogPost blogPost)
{
if (ModelState.IsValid)
{
blogPost.PublishDate = DateTime.Now;
_blogPostRepository.AddPost(blogPost);
_blogPostRepository.Save();
// if you want to return "Add" page you should
// initialize your viewbag and create model instance again
ViewBag.CategoryList = new SelectList(_categoryRepository.GetAllCategory(), "Id", "Name");
return View(new BlogPost());
}
return new HttpNotFoundResult("An Error Accoured while requesting your order!");
}

Related

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>

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 doesn't display selected item

I'm trying to get the drop down list to have my item selected when there is an item, but it never does. I've Googled this and tried many different methods, but they all seem to use a ViewModel containing the list instead of using ViewBag, but I would like to stick to the ViewBag if possible.
My controller:
[HttpGet]
public ActionResult Index(int? id)
{
ViewBag.SelectList = new SelectList(rep.GetItemList(), "id", "type");
if (id.HasValue)
{
var model = rep.GetItemByID(id.Value);
if ( model != null )
{
return View(model);
}
}
return View();
}
My View:
<div class="editor-field">
#Html.DropDownListFor(model => model.itemID, (SelectList)ViewBag.SelectList)
#Html.ValidationMessageFor(model => model.itemID)
</div>
This doesn't have my item selected in the DropDownList, and I've also tried having a list in the ViewBag and then constructing the SelectList in the View, which some posts say should solve the problem:
<div class="editor-field">
#Html.DropDownListFor(model => model.itemID, new SelectList(ViewBag.SelectList, "id", "type", Model.itemID))
#Html.ValidationMessageFor(model => model.itemID)
</div>
But none of it seems to work. So I was wondering if there is anyone where that is able to spot what I'm doing wrong?
make sure your itemID property is set in the model you are passing to the view
if (id.HasValue)
{
var model = rep.GetItemByID(id.Value);
model.itemID=id.Value;
return View(model);
}
I would try setting the selected value from the begining since SelectList is immutable.
[HttpGet]
public ActionResult Index(int? id)
{
if (id.HasValue)
{
ViewBag.SelectList = new SelectList(rep.GetItemList(), "id", "type", id );
var model = rep.GetItemByID(id.Value);
if ( model != null )
{
return View(model);
}
}
else
{
ViewBag.SelectList = new SelectList(rep.GetItemList(), "id", "type");
}
return View();
}
In your View use it like this:
#Html.DropDownListFor(model => model.itemID, (SelectList)ViewBag.SelectList, "Please select...")

failed in data insertion in asp.net mvc3

i've a table post(p_id,u_id(FK),date,title,type,viewer)and a Modelclass UserModel.cs with(p_id,u_id,date,title,type). When i to create a new post in a post controller i tried like public ActionResult Create(PostModel p)
{
if (ModelState.IsValid)
{
p.date = DateTime.Today;
post post = new Models.DB.post();
post.u_id = User.Identity.Name;
post.date = p.date;
post.title = p.title;
post.type= p.type;
post.viwer=1;
try{ db.posts.AddObject(post);
db.SaveChanges();
return RedirectToAction("LoggedIndex");
}
catch{ return RedirectToAction("Index"); }
}
return View(p);
}
It is not Working. I think from is not submitting when i submit always view is returned. i i've created stronly typed create view. in create.cshtml i've two hidden field user_id and date of postModel Object. I'm very new at MVC.please help me thanks in advance....
Sending Data from View to the Controler by using the MVC-Model
I understand it like that: you have a mvc3 page and want to send some custom data to your controller:
Controller:
[http-post]
public ActionResult Create(PostModel p)
{
if (ModelState.IsValid)
{
p.date = DateTime.Today;
post post = new Models.DB.post();
post.u_id = User.Identity.Name;
post.date = p.date;
post.title = p.title;
post.type= p.type;
post.viwer=1;
db.posts.AddObject(post);
db.SaveChanges();
return RedirectToAction("LoggedIndex");
}
return View(p);
}
public ActionResult Create()
{
return View(new post());
}
View:
#model post
#using (Html.BeginForm("Create", "Controller-Name", FormMethod.Post, new { id = "FORM" }))
{
#Html.EditorFor(model => model.date)
#Html.EditorFor(model => model.name)
#Html.EditorFor(model => model.title)
#Html.EditorFor(model => model.type)
}
Further informations: http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-preview-1.aspx

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