1

I want two CheckBoxList an index view, those CheckBoxList dynamically bind from two different tables. One is Sports and another is Country.

Basically i try through EditorTemplates, but it not working with two models. i face problem to use two model in a single view.

This is my Index Method in Controller

    SampleDBContext db = new SampleDBContext();
    public ActionResult Index()
    {
        ViewData["Sports"] = db.Sports.ToList();
        ViewData["Country"] = db.Countries.ToList();             
        return View(ViewData["Sports"]);// I am confused and i don't know what to write there we can call both table data.
    }

Template View for Sports

@model MVCDEMO.Models.Sports

@Html.HiddenFor(x => x.s_Id)
@Html.CheckBoxFor(x => (bool)x.is_selected)
@Html.DisplayFor( x => x.s_Name)

Template View for Country

@model MVCDEMO.Models.Country

@Html.HiddenFor(x => x.c_Id)
@Html.CheckBoxFor(x => (bool)x.is_selected)
@Html.DisplayFor( x => x.c_Name)

Index View

<div class="form-horizontal">
    <div class="form-group">
        <label class="col-sm-2 control-label">
            Select Sports
        </label>
        <div class="col-md-3">
            @Html.EditorForModel()//what to write it recognize Sports template
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">
            Select Country
        </label>
        <div class="col-md-3">
            @Html.EditorForModel()//what to write it recognize Country template
        </div>
    </div>
</div>

If i work for single Model it work perfectly.

2

1 Answer 1

1

I would personaly use a strongly typed view model having Sports and Countries as properties, instead of using ViewData:

public class IndexPageViewModel
{
    public IEnumerable<Sports> Sports { get; set; }
    public IEnumerable<Country> Countries { get; set; }        
}

Creating two partial views, one for each class (Sport and Country), would allow you to use the @Html.EditorForModel() helper. Reusing the partial views would also be an added benefit, but this obviously depends on the specific application.

Please note that the 'EditorForModel' helper will render an editor for each property of the model. If you need to have a different kind of editor, for example a dropdown menu, I'm not sure EditorForModel should be used.

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

3 Comments

if i use @model IEnumerable<CHBox.Models.PageNameViewModel> in view than how i target specific model for @html.EditorForModel(). I try but i fail...
You should use @model IndexPageViewModel in view and @model Sport in partial view.
IndexPageViewModel is just a name I've made up for the view model to contain the two lists, you may rename it if you like.

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.