简体   繁体   中英

How to pass Model to View in C# MVVC using MVC Razor

I'm new using MVC Razor and I have this problem.

I have a View like this:

@model MoldCapThickness.Models.MoldCapModels
@using PagedList.Mvc;
@using PagedList;
@{
    ViewBag.Title = "Index";
}
@Styles.Render("~/Content/MoldCapStyles.css")
@Scripts.Render("~/bundles/moldCapScript")
@using (Html.BeginForm())
{
    <div class="container">

        <div class="fields-field">
            @Html.LabelFor(m => m.LotNumber)
            @Html.TextBoxFor(m => m.LotNumber, new { title = "" })
            @Html.LabelFor(m => m.Equipment)
            @Html.TextBoxFor(m => m.Equipment, new { title = "" })
            <input type="submit" value="Buscar" name="Filter" />
        </div>
        <h4>Informacion del Lote seleccionado</h4>
        <div class="lotInfo">
            <p class="lotInfoLabel">
                @Html.LabelFor(m => m.PartNumber)
                @Html.DisplayTextFor(m => m.PartNumber)
            </p>
            <p class="lotInfoLabel">
                @Html.LabelFor(m => m.Equipment)
                @Html.DisplayTextFor(m => m.Equipment)
            </p>
            <p class="lotInfoLabel">
                @Html.LabelFor(m => m.MoldCapThickness)
                @Html.DisplayTextFor(m => m.MoldCapThickness)
            </p>
            <p class="lotInfoLabel">
                @Html.LabelFor(m => m.MoldCapThicknessRangeMax)
                @Html.DisplayTextFor(m => m.MoldCapThicknessRangeMax)
            </p>
            <p class="lotInfoLabel">
                @Html.LabelFor(m => m.MoldCapThicknessRangeMin)
                @Html.DisplayTextFor(m => m.MoldCapThicknessRangeMin)
            </p>
            <p class="lotInfoLabel">
                @Html.DisplayTextFor(m => m.EquipmentType)
            </p>
        </div>
        <div class="press-containers">
            @foreach (var press in (Model != null ? Model.TransferConfigurations : new List<MoldCapThickness.Models.TransferConfiguration>()))
            {
                <div class="press-par">
                    <div class="press">
                        <p class="press-name">@press.PressName</p>
                        <p>LEFT</p>
                        <p>Molde: @press.Mold</p>
                        @Html.LabelFor(m => press.NumeroTira)
                        @Html.TextBoxFor(m => press.NumeroTira)
                        <div id="press-thickness-list">
                            @foreach (var thickness in press.Thickness)
                            {
                                <div class="moldThicknessListItem">
                                    <label>@thickness.Name: </label>
                                    @Html.TextBoxFor(m => thickness.Value)
                                    <label>mm</label>
                                </div>
                            }
                        </div>
                        <div class="comments-container">
                            <a href="#">Comentarios</a>
                        </div>
                    </div>
                    <div class="press">
                        <p class="press-name">@press.PressName</p>
                        <p>RIGHT</p>
                        <p>Molde: @press.Mold</p>
                        @Html.LabelFor(m => press.NumeroTira)
                        @Html.TextBoxFor(m => press.NumeroTira)
                        <div id="press-thickness-list">
                            @foreach (var thickness in press.Thickness)
                            {
                                <div class="moldThicknessListItem">
                                    <label>@thickness.Name: </label>
                                    @Html.TextBoxFor(m => thickness.Value)
                                    <label>mm</label>
                                </div>
                            }
                        </div>
                    </div>
                </div>
            }
            @foreach (var press in (Model != null ? Model.CompressionConfiguration : new List<MoldCapThickness.Models.CompressionConfiguration>()))
            {
                <div class="press-par">
                    <div class="press">
                        <p class="press-name">@press.PressName</p>
                        <p>UPPER</p>
                        <p>Molde: @press.Mold</p>
                        <div>
                            @Html.LabelFor(m => press.NumeroTira)
                            @Html.TextBoxFor(m => press.NumeroTira)
                        </div>
                        <div id="press-thickness-list">
                            @foreach (var thickness in press.Thickness)
                            {
                                <div class="moldThicknessListItem">
                                    <label>@thickness.Name: </label>
                                    @Html.TextBoxFor(m => thickness.Value)
                                    <label>mm</label>
                                </div>
                            }
                        </div>
                    </div>
                    <div class="press">
                        <p class="press-name">@press.PressName</p>
                        <p>LOWER</p>
                        <p>Molde: @press.Mold</p>
                        <div>Numero de tira</div>
                        <div id="press-thickness-list">
                            @foreach (var thickness in press.Thickness)
                            {
                                <div class="moldThicknessListItem">
                                    <label>@thickness.Name: </label>
                                    @Html.TextBoxFor(m => thickness.Value)
                                    <label>mm</label>
                                </div>
                            }
                        </div>
                    </div>
                </div>


            }

        </div>
        <div>
            <input type="submit" value="Enviar" name="Send" />
        </div>
    </div>
}

The View is using the next Model:

public class MoldCapModels {
  [Display(Name = "Escanear Lote:")]
  public string LotNumber {
    get;
    set;
  }
  [Display(Name = "Numero de Parte:")]
  public string PartNumber {
    get;
    set;
  }
  [Display(Name = "Maquina:")]
  public string Equipment {
    get;
    set;
  }
  [Display(Name = "Mold Cap Thickness:")]
  public float MoldCapThickness {
    get;
    set;
  }
  [Display(Name = "MAX:")]
  public float MoldCapThicknessRangeMax {
    get;
    set;
  }
  [Display(Name = "MIN:")]
  public float MoldCapThicknessRangeMin {
    get;
    set;
  }
  private string _EquipmentType;
  public string EquipmentType {
    get {
      return _EquipmentType;
    }
    set {
      if (value == "INJECTION") {
        _EquipmentType = "TRANSFER";
      } else {
        _EquipmentType = value;
      }

    }
  }
  public int MoldThicknessCap {
    get;
    set;
  }
  public List < TransferConfiguration > TransferConfigurations {
    get;
    set;
  } = new List < TransferConfiguration > ();
  public List < CompressionConfiguration > CompressionConfiguration {
    get;
    set;
  } = new List < CompressionConfiguration > ();
  public string DisplayModal {
    get;
    set;
  }
}

In the view I have two inputs, Filter and Send, each one calls a method in the controller. The controller looks like this:

public class Controller: CommonController {
  public ActionResult Index() {
    return View();
  }

  [HttpPost]
  [ButtonClick(Name = "Filter")]
  public ActionResult Filter(MoldCapModels capModels) {
    if (!string.IsNullOrWhiteSpace(capModels.LotNumber) && !string.IsNullOrWhiteSpace(capModels.Equipment)) {
      capModels.PartNumber = GetPartNumber(capModels.LotNumber);
      capModels.Equipment = GetEquipment(capModels.Equipment);
      capModels.MoldCapThickness = GetMoldCapThickness(capModels.PartNumber);
      capModels.MoldCapThicknessRangeMin = GetMoldCapThicknessMinRange(capModels.PartNumber);
      capModels.MoldCapThicknessRangeMax = GetMoldCapThicknessMaxRange(capModels.PartNumber);
      capModels.EquipmentType = GetEquipmentType(capModels.Equipment);
      if (capModels.EquipmentType == "TRANSFER") {
        capModels.TransferConfigurations = GetTranserPresses(capModels.Equipment, capModels.PartNumber);
      } else {
        capModels.CompressionConfiguration = GetCompressionPresses(capModels.Equipment, capModels.PartNumber);
      }

    }
    return View(capModels);
  }

  [HttpPost]
  [ButtonClick(Name = "Send")]
  public ActionResult Send(MoldCapModels capModel) {
    return View(capModel);
  }

}

The filter method fills the fields of MoldCapModels and return the model with the fields filled to display them in the view. This is working fine. As you can see in the model I have two list of objects, the list populates depending of certain values on the data base, this list are handled in the view with the foreach cicles and each object of the view has its own values to fill represented as the thickness.Value field. The problem comes when I want to call the Send method using the Send input. The Send method is waiting the model used by the view but the problem is this model came with empty values and only the LotNumber and Equipment fields has values, the two list of the models and all the other fields are empty.

I need the Model values including the lists and its objects values.

How can I send the model with its current values to the Send method in the controller?

You need to update your View and in your foreach replace all textboxes that look like:

@Html.TextBoxFor(m => press.NumeroTira)

And use this:

<input type="textbox" name="capModel.TransferConfigurations[@index].NumeroTira" value="@press.NumeroTira"/>

or:

<input type="textbox" name="TransferConfigurations[@index].NumeroTira" value="@press.NumeroTira"/>

Depending of how the name attribute of other elements is being generated.

You will need to define @index before the foreach loop

@{var index = 0;}

And increment before the end of each iteration

@index++

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM