KZK
KZK

Reputation: 21

Pass multiple dictionaries in a model to a view?

I need some advise and help please. I am a newbie to MVC and programming in general.

Model:

 public class WorkflowStepsViewModel
{
    public Dictionary<string, string[]> stageData { get; set; }
    public Dictionary<string, List<string>> stepData { get; set; }
}

Controller:

public IActionResult Criteria(CriteriaViewModel model)
    {
        WorkflowStepsViewModel stepModel = new WorkflowStepsViewModel();
        if (ModelState.IsValid)
        {
            //call GetSteps class
            GetSteps steps = new GetSteps();
            var stageData = steps.GetStages(model);
            var stepData = steps.GetStep(stageData);
            stepModel.stageData = stageData;
            stepModel.stepData = stepData;
            ViewData["stepData"] = stepData;
            ViewData["stageData"] = stageData;
            return RedirectToAction("GetWorkflow", "GetWorkflow", ViewData);
        }
        else
        {
            //show error
            return View();

        }
    }

I want to use the values and keys in both the dictionaries in the view.. but I don't know how. Any help would be great. Should I split my model so that each model has 1 dictionary? I thought I could set the values in the model from controller and then use it in view.. but idk how I would call each key and value. TIA for your help.

Upvotes: 0

Views: 374

Answers (2)

KZK
KZK

Reputation: 21

Thank you Ryan Hansen. Looked at the link you provided and I was able to access the data. Below is the code for controller and view Controller

var svc_stageData = steps.GetStages(model);
var svc_stepData = steps.GetStep(svc_stageData);
//passing data to model
var stepModel = new WorkflowStepsViewModel
{
  stageData = svc_stageData,
  stepData = svc_stepData
};
return View(stepModel);

View (not my actual code. this just shows that data can be accessed.

@model WorkflowStepsViewModel

@{
    ViewData["Title"] = "Workflow";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@foreach (var stg in Model.stageData)
{
    @for (int i = 0; i < stg.Value.Length; i++)
    {
        <input type="button" name="@("stageData["+stg.Key+ "]")" value="@stg.Value[i]" />
    }
}
<h2>stepData</h2>

@foreach (var stp in Model.stepData)
{
    @for (int i = 0; i < stp.Value.Count; i++)
    {
        <h4>"@stp.Value.Count"</h4>
        <input type="button" name="@("stageData["+stp.Key+ "]")" value="@stp.Value[i]" />
    }
}

Upvotes: 0

Ryan Hansen
Ryan Hansen

Reputation: 141

There are a couple of issues with your sample; however, the question of "how to present dictionaries on a razor view" has already been asked and answered here: ASP.NET MVC Binding to a dictionary

Additionally, I am assuming that you are working with a newer version of ASP.NET MVC. It would be beneficial moving forward if you ensure that you were calling the version you are developing with. Posting your view code would also go a long way towards attaining a comprehensive answer to help you move forward.

As far as your other issues are concerned:

You are putting both collections into ViewData as well as the model class you've implemented. This is unnecessary. In this case, simply placing the collections into the model and binding the model to the view is all that it needed.

So long as you have defined the model within your view as the model class that you are giving as an example here, then the only thing you'll need to do is follow the instructions from the link I provided above.

Keep in mind that your references to the dictionaries will then be Model.stageData and Model.stepData within the loops in your view.

Upvotes: 1

Related Questions