2

強く型付けされたビューがあり、コントローラーでデータを取得したい。これは私が持っているものです:

@model WordAutomation.Models.Document

@{
    ViewBag.Title = "Document";
}

<h2>Document</h2>

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

    <fieldset>
        <legend>Document</legend>

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

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


@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

そして、コントローラーにはこれがあります:

[HttpPost]
        public ActionResult Document(FormCollection formValue)
        {
            string test = formValue[0].ToString();
            return View();
        }

しかしデータが出てこない。私は何が間違っているのですか?ありがとう

4

2 に答える 2

4

MVC にモデル バインディングを処理させます。コントローラーに渡す代わりに、FormCollection単にビュー モデルのインスタンスを渡すことができます。コントローラーのアクションを次のように変更します。

[HttpPost]
public ActionResult Document(WordAutomation.Models.Document model)
{
    string test = model.CaseNumber;

    return View(model); // return your model back to the view to persist values
}

FormCollectionMVC は、 toの値を自動的にバインドしWordAutomation.Models.Documentます。次に、POST 後にモデルをビューに戻すだけで、必要に応じて入力値を保持できます (これを例に含めました)。

于 2012-12-06T10:28:07.113 に答える
0

強く型付けされた場合、ビューからコントローラーにデータを取得するのはとても簡単です。必要なことは、model.yourpropertyname を介してコントローラーでデータを取得することだけです。

コードはモデルでは次のようになります

[HttpPost]
    public ActionResult Document(Document aDocumentModel)
    {
        string test = aDocumentModel.CaseNumber;
        return View();
    }

これで、データは「文字列テスト」で使用できるようになり、必要な場所で使用できます。また、「ドキュメント」モデルの名前空間についてコントローラーに伝える必要があるもう 1 つのことを追加する必要があります。

using WordAutomation.Models;//Your Model's namespace here

コントローラーの名前空間部分。

それが役に立てば幸い!!!

于 2012-12-06T13:24:21.950 に答える