내 모델과 내보기가 있습니다. 모든 것이 잘 작동합니다. 유일한 문제는 경고 기호가 예상대로 표시되지 않는 것입니다. 모델에 데이터 주석을 추가했습니다.POST 전에 모델이 유효하지 않음
필요한 필드가 비어있는 경우 오류가 발생합니다. 나는 뭔가가 엉망이라면 제출하지 않을 것이고 경고 신호를 보일 것이라고 기대한다.
[HttpPost]
public ActionResult Create(Recipe recipe, List<Ingredient> ingredient, List<Direction> direction)
나는 그것을하지 않습니다 그래서 지금 시도 캐치를 추가 :이 컨트롤러
@model RecipesBlog.Models.ViewModels.RecipeV
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Create Recipe</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Ingredient.Text, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@for (int i = 0; i < 3; i++)
{
<p> <input class="form-control" type="text" name="Ingredient[@i].Text"><br></p>
}
<div class="Ingredient"></div>
<input class="add_Ingredient" type="text" value="+" />
@Html.ValidationMessageFor(model => model.Ingredient.Text, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Direction.Text, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@for (int i = 0; i < 3; i++)
{
<p> <input class="form-control" type="text" name="Direction[@i].Text"><br></p>
}
<div class="Directions"></div>
<input class="add_Direction" type="text" value="+" />
@Html.ValidationMessageFor(model => model.Direction.Text, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
: 이것은 내이다
public partial class RecipeV
{
[Display(Name = "Title")]
[Required(ErrorMessage = "Title required")]
public string Title { get; set; }
[Required(ErrorMessage = "Description required")]
[StringLength(5)]
[Display(Name = "Description")]
public string Description { get; set; }
public IngredientV Ingredient { get; set; }
public DirectionV Direction { get; set; }
}
:
이
내 모델입니다 오류가 있으면 제출하십시오. try
{
recipe.Date = DateTime.Now;
// Add the new recipe to the recipes table.
db.Recipes.InsertOnSubmit(recipe);
db.SubmitChanges();
}
catch
{
return View();
}
int id = recipe.RecipeID;
foreach (Ingredient i in ingredient)
{
if (i.Text != null)
{
i.RecipeID = id;
db.Ingredients.InsertOnSubmit(i);
db.SubmitChanges();
}
}
foreach (Direction d in direction)
{
if (d.Text != null)
{
d.RecipeID = id;
db.Directions.InsertOnSubmit(d);
db.SubmitChanges();
}
}
//Direct the user to the index page.
return RedirectToAction("index", "Recipes", new { id = recipe.RecipeID });
}
? 그게 언제 나타날 거니? – Shyju
확인 경고 – coco