1

さて、私はMVCフレームワークを使用しています。モデルを追加するためのビューがあります。現在、デフォルトの「作成」コントローラーを使用しています。

自分の変数を事前に設定してモデルを作成できるようにしたいと思います。たとえば、model.UserIdをユーザーIDに設定します。いくつかの値をユーザーが入力し、いくつかをすでに設定しておく必要があります。このようなことをする方法はありますか

(擬似コード)

model.matchId = 123
model.prediction type = "user input"
add model 

これが私の現在のコードです

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
    <legend>Predictions</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.MatchId, "Match")
    </div>
    <div class="editor-field">
        @Html.DropDownList("MatchId", String.Empty)
        @Html.ValidationMessageFor(model => model.MatchId)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.UserId, "User")
    </div>
    <div class="editor-field">
        @Html.DropDownList("UserId", String.Empty)
        @Html.ValidationMessageFor(model => model.UserId)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Type)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Type)
        @Html.ValidationMessageFor(model => model.Type)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Prediction)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Prediction)
        @Html.ValidationMessageFor(model => model.Prediction)
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

}

4

1 に答える 1

0

コントローラーでは、モデルをビューに返す前にモデルに値を設定します。

public class HomeController : Controller
    {
        public ActionResult About()
        {
            var model = new MyModel();
            model.SomeId = 123;
            model.SomeOtherProperty = "Hello World";
            return View(model);
        } 
 }
于 2013-02-07T15:12:10.120 に答える