をコントローラにビューからモデルデータを渡しながら、私はちょうどMVC.NETフレームワークで構成モデルを作成したヌル値を取得し、コントローラに私の見解からそれを渡すだけヌル値を取得:ちょうど
モデル:
namespace MyApp.Models
{
public class ModelX
{
public String attributeX { set; get; }
}
}
namespace MyApp.Models
{
public class ModelY
{
public String attributeY { set; get; }
}
}
namespace MyApp.Models
{
public class MyComposedModel
{
public ModelX SubModel1 { set; get; }
public ModelY SubModel2 { set; get; }
}
}
CREATEVIEW:
@model Repo.Models.MyComposedModel
@{
ViewBag.Title = "Create";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.SubModel1.attributeX, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.SubModel1.attributeX, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.SubModel1.attributeX, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SubModel2.attributeY, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.SubModel2.attributeY, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.SubModel2.attributeY, "", new { @class = "text-danger" })
</div>
</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>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
コントローラー:
//...
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(MyComposedModel MCM)
{
if (ModelState.IsValid)
{
ModelX modX = new ModelX();
modX.attributeX = MCM.SubModel1.attributeX;
db.ModelX.Add(modX);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(MCM);
}
//...
私の間違いがどこにあるかを知る手伝いをすることができます。申し訳ありませんが、私は初心者です。私は完全に間違った方法でそれをやっていないことを願っています。お礼と事前に感謝!
表示されたコードは正常に動作します。 –
あなたの質問とは何の関係もない回答は受け入れないでください! –