MVC3 Dropdown selection based on other Drop down value - asp.net-mvc-3

Hi there im working on MVC3 and have problem with dropdown selection:
I have a table call CSystem and it hold value System1 and System2
another table call SystemModule hold System1Module and System2Module
But at dropdown selection at job view when System1 is selected the SystemModule still appear all value. Please help.
Controller:
public ActionResult Create()
{
ViewBag.CSystemID = new SelectList(db.CSystems, "CSystemID", "SystemName");
ViewBag.SystemModuleID = new SelectList(db.SystemModules.Where(x => x.CSystemID == CSystems.CSystemID), "SystemModuleID", "ModuleName");
return View();
}
View:
<div class="editor-label">
#Html.LabelFor(model => model.SystemModuleID, "SystemModule")
</div>
<div class="editor-field">
#Html.DropDownList("SystemModuleID", String.Empty)
#Html.ValidationMessageFor(model => model.SystemModuleID)
</div>

It appears you aren't passing in a model? I don't understand exactly what you are asking, but that is one glaring thing I can see in your code... hope this helps :\

Related

How to keep the same data when return to the view?

How to keep the same data when return to the view?
I tried to put return the form to the view, but it did not work.
Is there any good and simple way to do this?
[HttpPost]
public ActionResult Register(FormCollection form)
{
string name = form["Name"].Trim();
if (string.IsNullOrEmpty(name))
{
TempData["TempData"] = "Please provide your name ";
return View(form);
}
string email = form["Email"].Trim();
var isEmail = Regex.IsMatch(email, #"(\w+)#(\w+)\.(\w+)");
if (!isEmail)
{
TempData["TempData"] = "Sorry, your email is not correct.";
return View(form);
}
//do some things
}
Not sure why you would be using FormCollection in the post but maybe you come from a WebForms background. In MVC you should use ViewModels for the transport of your data to and from the Views.
By default the Register method in an MVC 3 app uses a ViewModel in the Register View. You should simply post it back. In fact, the default app has that already created for you if you didn't know as part of the Internet template.
The standard pattern is to have a ViewModel that represents your data that you will use in your View. For example, in your case:
public class RegisterViewModel {
[Required]
public string Name { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address")]
public string Email { get; set; }
}
Your controller the should contain 2 actions, a Get and a Post. The Get renders the View and is ready for the user to enter data. upon submitting the View the Post action is then called. The View sends the ViewModel to the action and the method then takes action to validate and save the data.
If there is a validation error with the data, it's very simple to return the ViewModel back to the View and display the error messages.
Here is the Get action:
public ActionResult Register() {
var model = new RegisterViewModel();
return View(model);
}
And here is the Post action:
[HttpPost]
public ActionResult Register(RegisterViewModel model) {
if(ModelState.IsValid) { // this validates the data, if something was required, etc...
// save the data here
}
return View(model); // else, if the model had validation errors, this will re-render the same view with the original data
}
Your view would look something like this
#model RegisterViewModel
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<div class="editor-label">
#Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.Name) <br />
#Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.Email) <br />
#Html.ValidationMessageFor(model => model.Email)
</div>
}
Using other strategies to capture and save data in an MVC app is absolutely possible, it's a very extensible framework. But there is a specific pattern that makes MVC what it is and working against that pattern can sometimes prove difficult. For a beginner it is best to understand the preferred patterns and strategies first and then once understood very well, you can then adopt some of your own custom strategies to meet your needs. By then you should understand the system well enough to know what you need to change and where.
Happy coding!!

Validation of DropDownListFor is not working in MVC3?

Validation is Working on Other Input type text element but not working on DropDownListFor
Class Purchase Input Property Code
[Required]
public string LedgerId { get; set; }
Class View Model Code
PurchaseViewModel purchaseVM = new PurchaseViewModel
{
// PurchaseInput=purchaseInput,
Ledger = uw.LedgerRepository.Get().Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.LedgerName }),
};
View
<div class="column">
<div class="labelField">
#Html.LabelFor(model => model.PurchaseInput.LedgerId, "Party")
</div>
<div class="ItemField">
#Html.DropDownListFor(model => model.PurchaseInput.LedgerId, new SelectList(Model.Ledger, "Value", "Text"))
#Html.ValidationMessageFor(model => model.PurchaseInput.LedgerId)
</div>
</div>
On the face of it, it seems that you do not have an empty item in your select list. The validation will only trigger if the user selects a dropdown item with string length of zero. If you examine the Html source can you see the validation attributes on the dropdown ( depending on whether you are using unobtrusive validation or not)?
Yes, there are problems with validation of DropDownListFor. look at this link. They get validation data manually from metadata - http://forums.asp.net/t/1649193.aspx
Although this is a workaround, at least it fires some sort of validation. Try:
#Html.DropDownListFor(model => model.PurchaseInput.LedgerId, new SelectList(Model.Ledger, "Value", "Text"), new { #class = "required" })

View or Hide a dropdownlist in my razor view using helper method

I have an object named Visit and i defined the following helper method (“CanBeEdited”) to specify if users can edit the object Status property or not:-
public partial class Visit
{
public bool CanBeEdited(string username)
{return (((DoctorID != null) && (DoctorID.ToUpper().Equals(username.ToUpper()))) && (StatusID == 5)); } }}
Then i have specified to show or hide certain dropdownlist on my Edit view depending on weather the CanBeEdited helper method returns true or false (if it returns true then the user can view and edit the Status dropdownlist, and if it returns false then the view will render an #Html.HiddenFor representing the old status value).
My edit view which includes the helper method looks as following:-
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<fieldset>
<legend>Visit</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Note)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Note)
#Html.ValidationMessageFor(model => model.Note)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.DoctorID)
</div>
<div class="editor-field">
#Html.DropDownList("DoctorID", String.Empty)
#Html.ValidationMessageFor(model => model.DoctorID)
</div>
#{
if (Model.CanBeEdited(Context.User.Identity.Name))
{
<div class="editor-label">
#Html.LabelFor(model => model.StatusID)
</div>
<div class="editor-field">
#Html.DropDownList("StatusID", String.Empty)
#Html.ValidationMessageFor(model => model.StatusID)
</div>
}
else
{
#Html.HiddenFor(model => model.StatusID)}
}
<p>
#Html.HiddenFor(model => model.VisitTypeID)
#Html.HiddenFor(model => model.CreatedBy)
#Html.HiddenFor(model => model.Date)
#Html.HiddenFor(model => model.VisitID)
#Html.HiddenFor(model => model.PatientID)
#Html.HiddenFor(model => model.timestamp)
<input type="submit" value="Create" />
</p>
</fieldset>
}
To be honest it is the first time i implement such as case,, so is my approach sound valid ???,, or it have some weaknesses i am unaware of ??. As i need to implemented similar cases all around my web application...
Baring in mind that i am also checking for the CanBeEdited on the action methods..
Thanks in advance for any help.
Updated:-
My post action method look as follow:-
[HttpPost]
public ActionResult Edit(Visit visit)
{
if (!(visit.Editable(User.Identity.Name)))
{
return View("NotFound");
}
try
{
if (ModelState.IsValid)
{
repository.UpdateVisit(visit);
repository.Save();
return RedirectToAction("Index");
}
}
catch (DbUpdateConcurrencyException ex)
{
var entry = ex.Entries.Single();
var clientValues = (Visit)entry.Entity;
ModelState.AddModelError(string.Empty, "The record you attempted to edit "
+ "was modified by another user after you got the original value. The "
+ "edit operation was canceled and the current values in the database "
+ "have been displayed. If you still want to edit this record, click "
+ "the Save button again. Otherwise click the Back to List hyperlink.");
// patient.timestamp = databaseValues.timestamp;
}
catch (DataException)
{
//Log the error (add a variable name after Exception)
ModelState.AddModelError(string.Empty, "Unable to save changes. Try again, and if the problem persists contact your system administrator.");
}
ViewBag.DoctorID = new SelectList(Membership.GetAllUsers(), "Username", "Username", visit.DoctorID);
ViewBag.StatusID = new SelectList(db.VisitStatus, "StatusID", "Description", visit.StatusID);
ViewBag.VisitTypeID = new SelectList(db.VisitTypes, "VisitTypeID", "Description", visit.VisitTypeID);
return View(visit);
}
I don't feel adding that in the View is a good idea. I would like to have My ViewModel to hold a property of boolean type to determine that it is editable or not. The value of that you can set in your controller after checking the relevant permissions.
public class ProductViewModel
{
public bool IsEditable { set;get;}
//other relevant properties
}
and controller action
public ActionResult GetProduct()
{
ProductViewModel objVM=new ProductViewModel();
objVm.IsEditable=CheckPermissions();
}
private bool CheckPermissions()
{
//Check the conditions and return true or false;
}
So view will be clean like ths
#if (Model.IsEditable)
{
//Markup for editable region
}
IMHO, it sounds valid enough.
UPDATE: removed irrelevant commentary, and edited to indicate a primary concern.
Now, taking a closer look, especially with the controller action, I strongly recommend that you eliminate the hidden fields (except the one that you need to re-load the record from your back end).
A savvy user can tamper with the hidden form data (all the form data) and your controller action will happily send it all back to the server.
In reality, you should post back only the fields that are permitted to be changed, rehydrate the record from the back end, and transfer the "editable" fields to the fresh copy. This also comes closer to addressing concurrent edit and stale record issues.

Passing a selected value from a partial view to the main view's viewmodel

As ASP.Net MVC3 newbies we have an issue that would like assistance with. (Questions at the bottom of this thread too.)
First of all I am not sure if the following is the best way to go about this so please let me know if we are heading in the wrong direction. We would like to use partial views to do lookups for dropdown lists. In some cases the lookups will be done in multiple places and also, the data is not part of our viewmodel. The data may be coming from a Database or Web Service in our application. Some of the data is loaded at startup and some is based upon other values selected in the form.
We are calling a child action from our main view and returning a partial view with the data we obtained. Once the user selects their choice we are not sure how to store the selected item code in our main view model.
In our main form we call to an action:
#model Apps.Model.ViewModels.AVMApplicationInfo
...
<div class="editor-label">
#Html.LabelFor(m => m.VMResidencyWTCS.DisplayState)
</div>
<div class="editor-field">
#Html.TextBoxFor(m => m.VMResidencyWTCS.DisplayState)
#Html.DropDownListFor(m => m.VMResidencyWTCS.DisplayState, Apps.Model.Helpers.ResidencyStates.StateList)
#Html.ValidationMessageFor(m => m.VMResidencyWTCS.DisplayState)
</div>
#Html.Action("DisplayCounties", "PersonalInfo")
...
In the PersonalInfo controller:
[ChildActionOnly]
public ActionResult DisplayCounties()
{
IList<County> countiesDB = _db.Counties
.OrderBy(r => r.CountyDescr)
.Where(r => r.State == "WI"
&& r.Country == "USA")
.ToList();
//Create an instance of the county partial view model
VMCounty countyView = new VMCounty();
//Assign the available counties to the view model
countyView.AvailableCounties = new SelectList(countiesDB, "CountyCd", "CountyDescr");
return PartialView("_DisplayCounties", countyView);
}
In the _DisplayCounties partial view:
#model Apps.Model.ViewModels.VMCounty
<div class="editor-label">
#Html.LabelFor(m => m.CountyDescr)
</div>
<div class="editor-field">
#Html.DropDownListFor(x => x.SelectedCountyCd, Model.AvailableCounties)
</div>
How do I assign the SelectedCountyCd to a field in the main form view model (Apps.Model.ViewModels.AVMApplicationInfo )? Are there any issues of when the child action/partial view is called; i.e., is it loaded at start up and can this method be used to include a user choice as a filter for the lookup? If so, how could the value be passed to the child controller; viewbag?
You could pass it as parameter to the child action:
#model Apps.Model.ViewModels.AVMApplicationInfo
...
#Html.Action("DisplayCounties", "PersonalInfo", new {
selectedCountyCd = Model.CountyCd // or whatever the property is called
})
and then have the child action take this value as parameter:
[ChildActionOnly]
public ActionResult DisplayCounties(string selectedCountyCd)
{
IList<County> countiesDB = _db.Counties
.OrderBy(r => r.CountyDescr)
.Where(r => r.State == "WI"
&& r.Country == "USA")
.ToList();
//Create an instance of the county partial view model
VMCounty countyView = new VMCounty();
//Assign the available counties to the view model
countyView.AvailableCounties = new SelectList(countiesDB, "CountyCd", "CountyDescr");
// assign the selected value to the one passed as parameter from the main view
countyView.SelectedCountyCd = selectedCountyCd;
return PartialView("_DisplayCounties", countyView);
}

ASP.NET MVC 3 #Html.DropDownListFor ignoring selectedValue

In my asp.net MVC 3 project I would like to create a contact that's related to a company.
You can either directly create a contact OR go via the company details view and add a new contact passing the companyId to set that company already in the dropdown on the contact create form.
The problem is that I can 't get the passed company as default in my dropdown.
Global.asax
routes.MapRoute("contactCreate", "contact/toevoegen/{companyid}", new { action = "ContactCreate", controller = "Backend", companyid = UrlParameter.Optional });
Controller method
public ActionResult ContactCreate(int? companyid)
{
Contact contact = new Contact();
ViewBag.StatusList = srep.getContactStatusses();
ViewBag.CompanyId = companyid;
return View(contact);
}
View
#model xxx.Models.Contact
...
<div class="editor-label">
#Html.LabelFor(model => model.bedrijf_id)
</div>
<div class="editor-field">
#Html.DropDownListFor(model => model.bedrijf_id, new SelectList(ViewBag.Bedrijven, "bedrijf_id", "bedrijf_naam",ViewBag.CompanyId), "--Kies bedrijf--")
#ViewBag.CompanyId
#Html.ValidationMessageFor(model => model.bedrijf_id)
</div>
...
#ViewBag.CompanyId has a value.
Any idea why it's not setting the selected value?
When doing a "DropDownListFor" it will try to match up the value passed in from the model for the selected value. So in your example it will use "bedrijf_id" as the selected value. It looks like you want the selected value to be from something outside of your model.
From the comments I think what you want is just a DropDownList as follows:
#Html.DropDownList("DropDownList", new SelectList((ViewBag.Bedrijven, "bedrijf_id", "bedrijf_naam", ViewBag.CompanyId), "--Kies bedrijf--")
Hope this helps.

Resources