MVC 3の作成ページにドロップダウンリストがありますが、私はそのページをPOSTするときに値を取得しません。次のように私のコードは: -MVC 3ドロップダウンリストに値がありません
ビュー: -
@using (Html.BeginForm("Create", "League", FormMethod.Post, new { enctype = "multipart/form-data" }))
{@ Html.ValidationSummary(真) リーグ
<div class="editor-label">
@Html.LabelFor(model => model.League.fk_CountryID, "Country")
</div>
<div class="editor-field">
@Html.DropDownList("fk_CountryID", "--Select One--") *
@Html.ValidationMessageFor(model => model.League.fk_CountryID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.League.LeagueName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.League.LeagueName)
@Html.ValidationMessageFor(model => model.League.LeagueName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.League.Image)
</div>
<div class="editor-field">
Upload File: <input type="file" name="Image" />
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
コントローラー: -
[HttpPost]
public ActionResult Create([Bind(Exclude = "Image")]Country country, HttpPostedFileBase Image, League league)
{
if (ModelState.IsValid)
{
model.League = league;
try
{
foreach (string file in Request.Files)
{
HttpPostedFileBase fileUploaded = Request.Files[file];
if (fileUploaded.ContentLength > 0)
{
byte[] imageSize = new byte[fileUploaded.ContentLength];
fileUploaded.InputStream.Read(imageSize, 0, (int)fileUploaded.ContentLength);
model.League.Image = imageSize;
}
}
db.Leagues.AddObject(model.League);
db.SaveChanges();
return RedirectToAction("Index");
}
catch (Exception e)
{
ModelState.AddModelError("uploadError", e);
}
}
私は間違って何をしていますか?コントローラーのドロップダウンリストの値を取得できません。次のようにあなたの助けと時間を
おかげ
をお使いのモデルがどのように見えるんどのように?なぜあなたのPOSTコントローラのアクションは非常に多くの引数を取るのですか?なぜビューモデルを使用していないのですか?モデルのどのコレクションプロパティにドロップダウンリストがバインドされていますか? –
私のモデルはEFモデルです。カントリーにはCountryID、CountryNameおよびImageがあります。リーグにはリーグID、fk_CountryID、リーグ名、イメージがあります。私のコントローラは画像、国、リーグを獲得しています。私はViewModelsを使用しています、model.League = league; 。私のドロップダウンリストはCountryモデルにバインドされています。 – Johann
あなたはビューモデルが何であるかについていくつかの誤解があるかもしれないと思います。ビューモデルには、ドメイン固有のオブジェクトが含まれていたり、参照されたりしてはなりませんあなたの 'Country'と' League'クラスはドメインモデルです。彼らは見解の中で何もしていません。ビューモデルは、ビュー用に特別に設計されたクラスです。私のViewModelに –