Reputation: 65
forgive me I am just learning mvc. I have an application that manages projects and utilities. For each project, there can be multiple utilities. There is a "Projects" page that lists all of the projects, and if you click on a Project, it lists all of the utilities associated with it. There is also a button on the project screen to allow you to add Utilities to that project. So, when you click on the project and then click the "Add Utility" button, it pulls up a form to allow you to add a utility to that project. The form has the project ID, utility ID and owner pre-filled, taken from the project controller information. What I am trying to do is set a default on one of the fields in the Utility (relocation expense) to 0.00. So, that, if it is not changed by the user, it shows as 0.00 in the database. Seems simple, right?
Here is my code in the controller currently for the Get method of the Utility (advice from a previous thread) and what I changed it to.
Before:
// GET: /Utility/Create
public ActionResult Create(int? ProjectID)
{
CreateDropDownListForCreateOrEdit(ProjectID);
return View();
}
After:
public ActionResult Create(int? ProjectID)
{
CreateDropDownListForCreateOrEdit(ProjectID);
// initialize the model and set its properties
UtilityDTO model = new UtilityDTO
{
Est_Relocation_Expense = 0M
};
// return the model
return View(model);
}
My view looks like this:
@Html.TextBoxFor(model => model.Est_Relocation_Expense, "{0:0.00}")
This works great...it adds the default value to the field...however, it loses the pre-filled project id, utility id, and owner information (does not pre-fill) that it retrieved from the project controller.
Does anyone know what might be wrong here? I can provide other code from the controller as well, if needed, but it is very long so not sure what else to post.
Added view for project id:
<div class="form-group">
@Html.LabelFor(model => model.Project_ID, new { @class = "control-label col-md-2 CreateEditFieldNamesSpan" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Project_ID, (SelectList)ViewBag.VBProjectIDAndName, "---Select Project---")
@Html.ValidationMessageFor(model => model.Project_ID)
</div>
</div>
Upvotes: 0
Views: 4651
Reputation:
You need to set the value of the properties in the model before you pass the model to the view. Your currently only setting at value for Est_Relocation_Expense
. If you want the dropdownlist to display the option associated with the ProjectID
parameter in your method, then modify your method to
public ActionResult Create(int? ProjectID)
{
CreateDropDownListForCreateOrEdit(ProjectID);
UtilityDTO model = new UtilityDTO
{
Project_ID = ProjectID, // add this
Est_Relocation_Expense = 0M
};
return View(model);
}
Side note: It is not necessary to send the value of ProjectID
to the CreateDropDownListForCreateOrEdit()
method. From your now deleted code, that was being used only to set the selectedValue
property of SelectList
which is ignored when binding a dropdownlist to a property. You need only use the constructor which is public SelectList(IEnumerable items, string dataValueField, string dataTextField)
Upvotes: 1