I can’t use two models in one with cshtml

Asked

Viewed 74 times

1

I need to load a dropdown, but the dropdown data comes from one model and cshtml is from another model. So I created a model that was common to both of us and I decided on this model, the two models that we needed as Property, in this way: Resellermodel:

public class ResellerModel
    {
        public int Id { get; set; }

        [Display(Name = "Nome")]
        public string Name { get; set; }

        [Display(Name = "Alias")]
        public string Alias { get; set; }

        [Display(Name = "Habilitada")]
        public bool Enabled { get; set; }

        [Display(Name = "MPN Id")]
        public string ResellerMpnId { get; set; }

        [Display(Name = "Usar página personalizada de fechamento de pedido")]
        public bool UseCustomSuccessPage { get; set; }

        [AllowHtml]
        public string CustomSuccessPage { get; set; }

        [Display(Name = "Google Analytics")]
        public string GA { get; set; }

        [Display(Name = "Home Template")]
        public int HomeTemplateId { get; set; }
        public ICollection<SelectListItem> HomeTemplateOptions { get; set; }

        [Display(Name = "Grupo de Desconto")]
        public int? AzureDiscountGroupId { get; set; }
        public ICollection<AzureDiscountGroup> AzureDiscountGroup { get; set; }
    }

Azurediscountgroupmodel(Not yet complete)

public class ResellerAzureDiscountGroupModel
    {
        [Display(Name = "Grupo de Desconto")]
        public int SelectedDiscountId { get; set; }

        public List<AzureDiscountGroup> Items { get; set; }

        //public ResellerAzureDiscountGroupModel(AzureDiscountGroup group)
        //{
        //    Items = new List<AzureDiscountGroup>();
        //    Items.Add(new AzureDiscountGroup { Id = group.Id, Descricao = group.Descricao, PercDesconto = group.PercDesconto });
        //}
    }

And the Model Common

public class ResellerDiscountComomModel
    {
        public ResellerModel Reseller { get; set; }
        public ResellerAzureDiscountGroupModel ResellerAzureDiscountGroup { get; set; }
    }

In cshtml I did so(I’m not going to put in full, just a few fields, because they are all the same

@model @model Meu_Path.API.Models.Resellers.ResellerDiscountComomModel

<h2>Editar revenda</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

<div class="form-horizontal">
    <hr />

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.Reseller.Id)

        <div class="form-group">
            @Html.LabelFor(model => model.Reseller.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Reseller.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Reseller.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Reseller.HomeTemplateId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.Reseller.HomeTemplateId, Model.Reseller.HomeTemplateOptions, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Reseller.HomeTemplateId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Reseller.Alias, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Reseller.Alias, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Reseller.Alias, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Reseller.Enabled, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <div class="checkbox">
                    @Html.EditorFor(model => model.Reseller.Enabled)
                    @Html.ValidationMessageFor(model => model.Reseller.Enabled, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Reseller.ResellerMpnId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Reseller.ResellerMpnId, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Reseller.ResellerMpnId, "", new { @class = "text-danger" })
            </div>
        </div>

        <p>Caso não seja preenchido vai estar com o GA Default UA-79444310-1, não colocar a tag &ltscript&gt &lt/script&gt</p>
        <div class="form-group">
            @Html.LabelFor(model => model.Reseller.GA, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextAreaFor(model => model.Reseller.GA, 11, 20, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Reseller.GA, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-10">
                <div class="checkbox">
                    @Html.CheckBoxFor(model => model.Reseller.UseCustomSuccessPage) @Html.LabelFor(model => model.Reseller.UseCustomSuccessPage)
                    @Html.ValidationMessageFor(model => model.Reseller.UseCustomSuccessPage, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            @Html.TextAreaFor(x => x.Reseller.CustomSuccessPage, new { htmlAttributes = new { @class = "form-control" } })
        </div>


        <div class="edit-submit-links">
            @Html.ActionLink("Voltar", "Index", null, new { @class = "btn btn-default" })
            <input type="submit" value="Salvar" class="btn btn-default" />
            <div class="clr"></div>
        </div>
    </div>
    }

This approach is returning me this error:

The model item passed into the Dictionary is of type 'Meu_path.API.Models.Resellers.Resellermodel', but this Dictionary requires a model item of type 'Meu_Path.API.Models.Resellers.ResellerDiscountComomModel'.

EDIT1

Just follow my controller

[HttpPost]
        public ActionResult Edit(ResellerModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                    return View(model);

                var reseller = _resellerService.GetById(model.Id);

                //Verifica o Alias
                var aliasReseller = _resellerService.GetByAlias(model.Alias);

                if (aliasReseller != null && aliasReseller.Id != reseller.Id)
                    throw new ApplicationException("Alias já cadastrado para a revenda Id " + aliasReseller.Id);

                reseller.Name = model.Name;
                reseller.Alias = model.Alias;
                reseller.Enabled = model.Enabled;
                reseller.ResellerMpnId = model.ResellerMpnId;
                reseller.HomeTemplateId = model.HomeTemplateId;

                _resellerService.Update(reseller);

                var whiteLabel = _whiteLabelService.GetLastByResellerId(reseller.Id);

                whiteLabel.CustomSuccessPage = model.CustomSuccessPage;
                whiteLabel.UseCustomSuccessPage = model.UseCustomSuccessPage;

                _whiteLabelService.Update(whiteLabel);

                _genericAttributeService.SetAttributeValue(whiteLabel, "GA", model.GA);

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex);
                return View(model);
            }
        }
  • I believe that you should put your classes all within one, and work them in the view from the class "father".

  • @Augustohenrique, I have two classes and both of them I put inside a single one, which is the Model Commom. It’s the group you’re talking about?

  • @Augustohenrique, I understand now what you meant. In my Model I have a ICollection<AzureDiscountGroup>, how would I carry this in the dropdown?

  • Why Viewbag would be a good option, approach?

  • How I give a new Resellerdiscountcomommodel() so that the two properties(classes) are not null?

  • You could make a parent class with the other daughter classes within it. So you call the classPai.classefilha.propriedadeClasseFile

  • I did it by Viewbag and it worked. I wouldn’t like it, I’d prefer it with models, but it didn’t work.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.