An article view model
public class ArticleViewModel : ViewModelBase
{
[Required(ErrorMessage = "Required")]
public string Title { get; set; }
[Required(ErrorMessage = "Choose the language")]
public BELocale Locale { get; set; }
}
public class BELocale : BEEntityBase
{
public string OriginalName { get; set; }
public string FriendlyName { get; set; }
public string TwoLetterISOName { get; set; }
}
A view "AddLocaleForArticle"
#model Models.ArticleViewModel
#using (Html.BeginForm("VefifyAddingLocaleForArticle", "Administration"))
{
#Html.TextBoxFor(m => m.Title, new { disabled = "disabled" })
#Html.DropDownListFor(m => m.Locale,
new SelectList(ViewBag.AvalaibleLocales, "ID", "OriginalName"), "Select a language"
)
#Html.ValidationMessageFor(m => m.Locale)
<input type="submit" value="Save" />
}
An action
public ActionResult VefifyAddingLocaleForPhoto(ArticleViewModel article)
{
//article.Locale == null for some reason.
//but article.Title isn't null, it contains the data
return RedirectToAction("AddingLocaleForPhotoSuccess", "adminka");
}
Why article.Locale is equal null and how to fix it?
When the form is submitted a dropdown list sends only the selected value to the controller. So you cannot expect it to populate an entire complex object such as BELocale using a dropdownlist. The best you could is to populate its ID property and fetch the remaining of the object from your data store using this id.
So you will have to modify your dropdownlist helper so that it is bound to the id property of the locale as first argument:
#Html.DropDownListFor(
m => m.Locale.ID,
new SelectList(ViewBag.AvalaibleLocales, "ID", "OriginalName"),
"Select a language"
)
Now inside the corresponding controller action you will get the id:
public ActionResult VefifyAddingLocaleForPhoto(ArticleViewModel article)
{
// article.Locale.ID will contain the selected locale id
// so you can use this information to fetch the corresponding BELocale object
...
}
You may fill dropdown like this in your view model
public List<KeyValuePair<int, String>> locale
{
get
{
return _localerepo.Findlocals().Select(x => new KeyValuePair<int, string>(x.ID, x.OriginalName)).ToList();
}
}
In your view use this
<%:Html.DropDownListFor(x => x.ID, new SelectList(Model.locale, "key", "value"), "--Select locale--")%>
<%= Html.ValidationMessageFor(model => model.ID)%>
Related
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>
I am having strange issue, MVC dropdown selected value is not preselected on page Load.
My Models are:
public class DocumentTypesViewModel
{
[Required(ErrorMessage = "DocumentType is required")]
public int OHDocumentTypeId { get; set; }
public string OHDocumentTypeDescription { get; set; }
}
public class ClientAdvancedSearchViewModel
{
[Display(Name = "Name")]
public string Name { get; set; }
[Display(Name = "DocumentType")]
public string DocumentTypeId { get; set; }
public IEnumerable<SelectListItem> DocumentTypes { get; set; }
}
In My Controllers I am populating the ClientAdvancedSearchViewModel like this
[HttpGet]
public ActionResult ClientAdvancedSearch()
{
ClientAdvancedSearchViewModel clientAdvancedSearchViewModel = iClientReferralRecordsRepository.GetDocumentMetadata();
//DocumentTypes Dropdown
var ddlDocumentTypes = iDocumentTypeRepository.GetDocumentTypes();
clientAdvancedSearchViewModel.DocumentTypes = new SelectList(ddlDocumentTypes, "OHDocumentTypeId", "OHDocumentTypeDescription",clientAdvancedSearchViewModel.DocumentTypeId);
return View(clientAdvancedSearchViewModel);
}
Finally in the View:
<td>
<div class="editor-label">
#Html.LabelFor(model => model.DocumentTypes)
</div>
<div class="editor-field">
#Html.DropDownListFor(x => x.DocumentTypeId, Model.DocumentTypes, "Please Select", new { #id = "ddlDocumentType" })
</div>
</td>
I believe the Name of the dropdown is same is x => x.DocumentTypeId, becuase of this I think, my value is not preselected.
This is the ViewSource for generated HTML for the Drop Down
<select id="ddlDocumentType" name="DocumentTypeId">
<option value="">Please Select</option>
<option value="20">records</option>
<option value="21"> record1</option>
..
How can I rename my dropdownlist name or How can I solve my problem?
Thank you
Updated: Added the missed line
ClientAdvancedSearchViewModel clientAdvancedSearchViewModel = iClientReferralRecordsRepository.GetDocumentMetadata();
Your code on your view is just right. You forgot to set the value for DocumentTypeId. This is your code as you posted:
[HttpGet]
public ActionResult ClientAdvancedSearch()
{
//DocumentTypes Dropdown
var ddlDocumentTypes = iDocumentTypeRepository.GetDocumentTypes();
clientAdvancedSearchViewModel.DocumentTypes = new SelectList(ddlDocumentTypes, "OHDocumentTypeId", "OHDocumentTypeDescription",clientAdvancedSearchViewModel.DocumentTypeId);
return View(clientAdvancedSearchViewModel);
}
And you missed this:
clientAdvancedSearchViewModel.DocumentTypeId = some_value;
Also, do you intend to have DocumentTypeId as an int instead of a string?
UPDATE:
You can also check that you set the id like this:
#Html.DropDownListFor(x => x.DocumentTypeId, new SelectList(Model.DocumentTypes, "Id", "Value", Model.DocumentTypeId), new { #id = "ddlDocumentType" })
Notice I used the overload with new SelectList. I don't remember all the overloads and I do it like that all the time, so you might check our the other overloads that suits your need.
I m working with asp.net mvc3 to create a list of radiobuttons. I need to get a list from the controller and display it in the view. For each corresponding list in the view I have YES/NO radio button and need to generate a string at the end saying for each item in the list if the radiobutton is yes then 1 else 0.
So for example if I have 10 items in the list then I need to display them in the view (default values of the items being false) and then generate a string on submit where each character in the string corresponds to the bool value of each item in the list.
Can anyone give me an idea how do I do this in mvc3 ?
Thanks for the help in advance.
UPDATE
Here is the code I am trying :
My class has two properties :
public List<Module> lstModules { get; set; } // gives the list of items
public List<bool> IsModuleActivelst { get; set; } //gives the bool values
Here in controller I need to create a list and the corresponding bool values for that. I am stuck here and not able to generate the code. Anyways I ll explain the pseudocode
public class MMController : Controller
{
[HttpGet]
public ActionResult Clients()
{
//I need to generate the list - using lstModules prop
// Assign the list with the predefined values and if not just give the default values- using IsModuleActivelst prop
}
}
Here I create the view :
#foreach (var i in Model.lstModules)
{
<div class="formlabel">
<div align="right" class="label">
#Html.LabelFor(model => model.lstModules):</div>
</div>
<div class="formelement">
<label for="radioYes" class="visible-enable" style="cursor:pointer;position:relative;">
#Html.RadioButtonFor(model => model.IsModuleActivelst, "True", new { #id = "radioYes", #style = "display:none;" })
<span>Yes</span></label>
<label for="radioNo" class="visible-disable" style="cursor:pointer;position:relative;">
#Html.RadioButtonFor(model => model.IsModuleActivelst, "False", new { #id = "radioNo", #style = "display:none;" })
<span>No</span></label>
</div>
}
I would recommend you to start by defining a view model that will represent the information that you need to be working with in the particular view. So far you have mentioned a list of modules where the user needs to set whether a module is active or not using a radio button (personally I would use a checkbox for True/False status but that's your own decision):
public class ModuleViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
}
public class MyViewModel
{
public IEnumerable<ModuleViewModel> Modules { get; set; }
}
Then you could define a controller that will populate the view model, render the form and have another action to handle the form submission:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new MyViewModel
{
// TODO: this information could come from a database or something
Modules = new[]
{
new ModuleViewModel { Id = 1, Name = "module 1", IsActive = true },
new ModuleViewModel { Id = 2, Name = "module 2", IsActive = true },
new ModuleViewModel { Id = 3, Name = "module 3", IsActive = false },
}
};
return View(model);
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
return Content(
string.Format(
"Thank you for selecting the following values: {0}",
string.Join(" ", model.Modules.Select(x => string.Format("model id: {0}, active: {1}", x.Id, x.IsActive)))
)
);
}
}
The last part is to define the view (~/Views/Home/Index.cshtml):
#model MyViewModel
#using (Html.BeginForm())
{
#Html.EditorFor(x => x.Modules)
<button type="submit">OK</button>
}
And finally the corresponding editor template that will automatically be rendered for each element of the Modules collection - notice that the name and location of the template is important - ~/Views/Shared/EditorTemplates/ModuleViewModel.cshtml:
#model ModuleViewModel
<div>
#Html.HiddenFor(x => x.Id)
#Html.HiddenFor(x => x.Name)
<h2>#Html.DisplayFor(x => x.Name)</h2>
#Html.Label("IsActiveTrue", "Yes")
#Html.RadioButtonFor(x => x.IsActive, "True", new { id = Html.ViewData.TemplateInfo.GetFullHtmlFieldId("IsActiveTrue") })
<br/>
#Html.Label("IsActiveFalse", "No")
#Html.RadioButtonFor(x => x.IsActive, "False", new { id = Html.ViewData.TemplateInfo.GetFullHtmlFieldId("IsActiveFalse") })
</div>
In a MVC application, I have a view showing tasks. The tasks should be filtered by employees to which they are assigned. Employees are shown in a dropdownlist.
#using (Html.BeginForm("EmployeeDropUsed", "Task", new { id = ?? }))
{
#Html.DropDownList(
"EmployeeDrop", new SelectList(Model.Employees, "Id", "EmployeeName"), "Select employee",
new
{
onchange = #"
var form = document.forms[0];
form.submit();"
})
}
I have a TaskController with an ActionResult, taking the data from the form:
[HttpPost]
public ActionResult EmployeeDropUsed(int id)
{
Employee e = er.GetById(id);
return LoadControls(er.GetAll(), e.Tasks.ToList());
}
The ViewModel looks like this
public class TaskModel
{
public TaskModel(List<Employee> employees, List<Task> tasks)
{
Employees = employees;
Tasks = tasks;
}
public List<Task> Tasks { get; set; }
public List<Employee> Employees { get; set; }
}
The problem is I donĀ“t know how to get the selected value from the drop back to the controller. I tried using DropDownListFor and an EmployeeId property in the model:
#Html.DropDownListFor(m => Model.EmployeeId, Model.Employees, "Select Employee")
public int EmployeeId { get; set; }
The result is that id is always 0.
Isn't that lambda expression incorrect though? Shouldn't it be
#Html.DropDownListFor(m => m.EmployeeId ....
instead of
#Html.DropDownListFor(m => Model.EmployeeId
The problem is that your EmployeeDropUsed action is looking for a parameter named id and the parameter you are sending it is EmployeeId. Either change the parameter name on the EmployeeDropUsed action to employeeId or change the EmployeeId property on TaskModel to id.
If you change the TaskModel property this should work:
#Html.DropDownListFor(m => m.id, Model.Employees, "Select Employee")
If you change the EmployeeDropUsed parameter this should work:
#Html.DropDownListFor(m => m.EmployeeId, Model.Employees, "Select Employee")
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" />
}