0

I've a main view that contains an array received from the corresponding Action and it also contains a partial view reference below

Create.cshtml :

@model HrAndPayrollSystem.Models.EmployeeMasterA

@using (Html.BeginForm())
{
    ViewData["fs_lbls"] = ViewBag.FS_lbls as string[];
    @Html.Partial("~/Views/EmployeeMasterA/FinalSettlementTAB.cshtml", Model)
}

and the referenced partial view above is defined below

FinalSettlementTAB.cshtml :

@model HrAndPayrollSystem.Models.EmployeeMasterA

    @Html.DropDownList("DeptId", null, "Department")   

/* Print "ViewData["fs_lbls"]" array defined in the Main View `Create.cshtml` here */

I've an array defined in the Create.cshtml, now, I want to pass it into the partial view HR_EmployeeFinalSettlementTAB.cshtml and print it, What is the proper way to to this?

What I've tried :

I changed the @Html.Partial() line into below :

 @Html.Partial("~/Views/EmployeeMasterA/FinalSettlementTAB.cshtml", null, new ViewDataDictionary { { "fs_lbls", ViewData["fs_lbls"] } })

and modified the FinalSettlementTAB.cshtml file as below :

@model HrAndPayrollSystem.Models.EmployeeMasterA

@Html.DropDownList("DeptId", null, "Department")   

@foreach (var i in ViewData["fs_lbls"] as string[])
{
    @i
}

But it throws an exception InvalidOperationException at line @Html.DropDownList("DeptId", null, "Department") by saying :

There is no ViewData item of type 'IEnumerable' that has the key 'DeptId'.

It throws the above exception whenever I try to pass the array data to the partial view using ViewDataDictionary, otherwise, it is working fine, when I'm not.

How do I get rid of the above exception and properly pass array data from main view to the partial view?

1 Answer 1

1

I suggest that you add a new property to EmployeeMasterA to store the labels, so that you do not need to use ViewData at all.

public class EmployeeMasterA
{
    public string[] fs_lbls { get; set; }

    public string SelectedLabel { get; set; }

    public List<SelectListItem> Labels
    {
        get
        {

            if (this.fs_lbls == null)
            {
                return Enumerable.Empty<SelectListItem>().ToList();
            }

            return (from label in fs_lbls
                   select new SelectListItem
            {
                Text = label,
                Value = label
            }).ToList();
        }
    }
}

Create.cshtml

@model WebApplication1.Controllers.EmployeeMasterA

@using (Html.BeginForm())
{
    @Html.Partial("FinalSettlementTAB", Model)
    <input type="submit" value="Save"/>
}

FinalSettlementTAB.cshtml

@model WebApplication1.Controllers.EmployeeMasterA

@Html.DropDownList("SelectedLabel", Model.Labels)

Controller

public ActionResult Create()
{
    var viewModel = new EmployeeMasterA();
    viewModel.fs_lbls = new[] {"Label1", "label 2"};

    return View(viewModel);
}

[HttpPost]
public ActionResult Create(EmployeeMasterA viewModel)
{
    return View();
}

You can set the content of fs_lbls in the controller action method, before returning the Create view. When you post the form, the SelectedLabel property will contain the selected item from the dropdown list. Obviously you will need to change the property names to suite your needs, but hopefully this will give you an idea.

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

Comments

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.