0

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>
16
  • You need to show the code for the CreateDropDownListForCreateOrEdit method. Commented Nov 24, 2015 at 23:32
  • 1
    What does this have to do with Java? Commented Nov 24, 2015 at 23:38
  • There is nothing in the code you have shown that could cause you to lose the values for project ID, utility ID and owner since nothing ever sets them. Show the relevant sections of the view where you generate the controls for each of those properties Commented Nov 25, 2015 at 0:50
  • @StephenMuecke not sure how much I can post...can I post the whole file? I can't find where it is assigning these. It is sorting the data various ways based on options chosen in the form to display by each field. And, displaying different data based on owner...I removed that code....just a bunch of "case" sections. Commented Nov 25, 2015 at 1:09
  • You have case statements inside a view??. Start by just showing how you generate the dropdownlist for the ProjectID property and we can work from there Commented Nov 25, 2015 at 1:10

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks again for all of your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.