开发者

DropDownListFor SelectedValue and Disable using Session State

开发者 https://www.devze.com 2023-04-10 23:45 出处:网络
I have been introduced to Razor as applied with MVC 3 this morning, so please forgive me if my question seems terribly uninformed!

I have been introduced to Razor as applied with MVC 3 this morning, so please forgive me if my question seems terribly uninformed!

I am working with an app whose workflow involves allowing a user to select a value (warehouse) from a drop down list, and add a record (material) from that warehouse to another record (Materials Request). Once the first material has been added to the Materials Request, I need to permanently set the value of the drop down to the warehouse that was first selected, then disable the drop down control (or set to read only, perhaps). The existing code in the razor file uses the DropDownListFor() method, including a ViewBag collection of Warehouse records. I have seen discussions which suggest abandoning the ViewBag design, but honestly I don't have the desire to rewrite major portions of the code; at least it looks like a major rewrite from the perspective of my experience level. Here's the original code:

@Html.LabelPlusFor(m => m.WarehouseId, "*:")
@Html.DropDownListFor(m => m.WarehouseId, (IEnumerable<SelectListItem>)ViewBag.WarehouseCodes, "")<br />

I believe I have been able to select a value based on a session object, though I'm still not sure how to disable the control. Here's my change:

    @{ 
        int SelectedWarehouseId = -1;
        if (HttpContext.Current.Session["SelectedWarehouseId"] != null)
        {
            SelectedWarehouseId = Int32.Parse(HttpContext.Current.Session["SelectedWarehouseId"].ToString());
        }
    }
    @Html.LabelPlusFor(m => m.WarehouseId, "*:")
    @{
        if (SelectedWarehouseId > -1)
        {
            @Html.DropDownListFor(m => m.WarehouseId, new SelectList((IEnumerable<SelectListItem>)ViewBag.WarehouseCodes, "WarehouseId", "WarehouseDescription", (int)SelectedWarehouseId))<br />
        }
        else
        {
            @Html.DropDownListFor(m => m.WarehouseId, (IEnumerable<SelectListItem>)ViewBag.WarehouseCodes, "")<br />
        }
    }

When the material is added to the Material Request, the WarehouseId is passed to the controller and I can access that value as "model.WarehouseId" in the controller class. However, I'm not sure how to get that value back to the View (apologies for the large code block here):

    [HttpPost]
    [TmsAuthorize]
    public ActionResult Create(ItemRequestViewModel model)
    {
        string deleteKey = null;
        //Removed code
        else if (Request.Form["AddToRequest"] != null)
        {
            // If the user clicked the Add to Request button, we are only
            // interested in validating the following fields. Therefore,
            // we remove the other fields from the ModelState.
            string[] keys = ModelState.Keys.ToArray();
            foreach (string key in keys)
            {
                if (!_addToRequestFields.Contains(key))
                    ModelState.Remove(key);
            }

            // Validate the Item Number against the database - no sense
            // doing this if the ModelState is already invalid.
            if (ModelState.IsValid)
            {
                _codes.ValidateMaterial("ItemNumber", model.ItemNumber, model.WarehouseId);
                Session["SelectedWarehouseId"] = model.Warehous开发者_运维问答eId;
            }

            if (ModelState.IsValid)
            {
                // Add the new Item Request to the list
                model.Items.Add(new ItemViewModel() { ItemNumber = model.ItemNumber, Quantity = model.Quantity.Value, WarehouseId = model.WarehouseId });
                ModelState.Clear();
                model.ItemNumber = null;
                model.Quantity = null;
                model.WarehouseId = null;
            }
        }
        //Removed code
        return CreateInternal(model);
    }

    private ActionResult CreateInternal(ItemRequestViewModel model)
    {
        if (model != null)
        {
            if (!String.IsNullOrEmpty(model.SiteId))
            {
                ViewBag.BuildingCodes = _codes.GetBuildingCodes(model.SiteId, false);
                if (!String.IsNullOrEmpty(model.BuildingId))
                    ViewBag.LocationCodes = _codes.GetLocationCodes(model.SiteId, model.BuildingId, false);
            }
            //Removed code
        }
        //Removed code
        ViewBag.WarehouseCodes = _codes.GetWarehouseCodes(false);
        return View("Create", model);
    }

So my questions are, how do I disable the drop down list, and how can I pass a value for the selected WarehouseId back to the view? I've also considered adding the value to the ViewBag, but to be honest I don't know enough about the ViewBag to recognize any unintended consequences I may face by just randomly modifying it's contents.

Thanks for any help offered on this.


Without going into which approach is better...

Your dropdown should be rendered as an HTML select element, in order to disable this you'll need to add a disabled="disabled" attribute to it.

The DropDownListFor method has a htmlAttributes parameter, which you can use to achieve this:

new { disabled = "disabled" }


when your pass model to your view like

return View("Create", model);

if WareHouseID is set in model then

Html.DropDownListFor(x=>x.WareHouseID, ...)

will automatically set the selected value and u don't have to do that session processing for this. So far as disabling a field is required, stewart is right. you can disable drop down this way but then it won't be posted to the server when u submit the form. you can set it to readonly mode like

new{@readonly = "readOnly"}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号