My entities:
public class Meal
{
[HiddenInput(DisplayValue = false)]
public int Id { get; set; }
[Required(ErrorMessage = "Proszę podać nazwę posiłku")]
public string Name { get; set; }
[Required(ErrorMessage = "Proszę podać ilość białka")]
[Range(0.00, double.MaxValue, ErrorMessage = "Proszę podać dodatnią ilość.")]
public double Protein { get; set; }
[Required(ErrorMessage = "Proszę podać ilość węglowodanów")]
[Range(0.00, double.MaxValue, ErrorMessage = "Proszę podać dodatnią ilość.")]
public double Carbohydrates { get; set; }
[Required(ErrorMessage = "Proszę podać ilość tłuszczy")]
[Range(0.00, double.MaxValue, ErrorMessage = "Proszę podać dodatnią ilość.")]
public double Fat { get; set; }
[Required(ErrorMessage = "Proszę podać ilość kalorii")]
[Range(0.00, double.MaxValue, ErrorMessage = "Proszę podać dodatnią ilość.")]
public double Calories { get; set; }
}
public class EatenMeal
{
public int Id { get; set; }
public virtual Meal Meal { get; set; }
public virtual MealType MealType { get; set; }
public double Serving { get; set; }
public string Username { get; set; }
public DateTime Date { get; set; }
}
public class MealType
{
public int Id { get; set; }
public string Name { get; set; }
}
In MealController's view MealList which displays meals from datebase. And there is a button "Add" which refers to action AddEatenMeal in EatenMealController.
public ActionResult AddEatenMeal(int id)
{
var meal = mealRepository.GetMeal(id);
EatenMeal eatenMeal = new EatenMeal() { Meal = meal, Username = User.Identity.Name };
return View(eatenMeal);
}
[HttpPost]
public ActionResult AddEatenMeal(EatenMeal eatenMeal)
{
if(ModelState.IsValid)
{
eatenMealRepository.AddEatenMeal(eatenMeal);
RedirectToAction("Index", "Home");
}
return RedirectToAction("Index", "Home");
}
I am creating there object EatenMeal and partially initializing this object. Then I am passing this object to View to further initializing.
@model Domain.Entities.EatenMeal
@{
ViewBag.Title = "Dodawanie posiłku do dziennika";
}
@using (Html.BeginForm("AddEatenMeal","EatenMeal", FormMethod.Post, new {@class = "form"}))
{
@Html.HiddenFor(x => x.Meal.Name)
@Html.HiddenFor(x => x.Username)
@Html.HiddenFor(x => x.Meal.Calories)
@Html.HiddenFor(x => x.Meal.Carbohydrates)
@Html.HiddenFor(x => x.Meal.Fat)
@Html.HiddenFor(x => x..Meal.Protein)
@Html.HiddenFor(x => x.Meal.Id)
@Html.HiddenFor(x=>x.Username)
<div class="form-group">
@Html.Label("Nazwa posiłku")
@Html.Label(Model.Meal.Name, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.Label("Porcja (g)")
@Html.TextBoxFor(x => x.Serving, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.Label("Typ posiłku")
@Html.DropDownListFor(x=>x.MealType)????
</div>
<div class="form-group">
@Html.Label("Data spożycia")
@Html.TextBoxFor(x => x.Date, new { @class = "form-control", @id="date-eaten", @Value=DateTime.Today.ToShortDateString()})
</div>
<input type="submit" class="btn btn-info" value="Dodaj" />
}
Now I have a question. Is it correct to hiding fields? I don't know how I can save data from first controller to second in other way. And is a second question. How I can make DropDownListFor for property MealTye in EatenMeal?