1
Answer

How to store Model in view to variable in Controller

Photo of Rijen Juni

Rijen Juni

9y
247
1
How to store editmodelfor to a variable so i can use in controller
 
example:
<div class="form-group">
@Html.LabelFor(model => model.total_card, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.total_card)
@Html.ValidationMessageFor(model => model.total_card)
</div>
</div>
i wanna store result of that code in a variable 
i was try:
var total_card= model=>model.total_card
but its still not works 
 

Answers (1)

0
Photo of Manas Mohapatra
NA 29.3k 3.3m 9y
If your model contain single object then follow below code:
 
@{
   string testVal = Model.total_card
}
 
if your model contain multiple objects like returning employee list then:
 
@model IEnumerable<BookStore.Models.Category>
 
@if (Model != null)
{
      foreach (var item in Model)
      {
             var total_card= item.total_card;
      }
}