0

mvc4 カミソリ ビューで httpget に空のモデルを返したいと思います。ユーザーがドロップダウンリストから値を選択したら、httppost でモデルをフェッチします。これは私が持っているものです:

 [HttpGet]
        public ActionResult AddNewAgent()
        {                   
            var modelAgentt = _fe.Agents.Take(1);           
            SelectList sl = new SelectList((from s in _aa.Agent.ToList() select new {COUNTER = s.COUNTER, FullName = s.LASTNAME + ", " + s.FIRSTNAME}), "COUNTER", "FullName", null);
            ViewBag.Agent= sl;
            return View(agg);
        }

それから HttpPost で私は持っています:

[HttpPost]
        public ActionResult AddNewAgent(int? LamcomAgents)
        {    
            var modelAgent = _fe.Agents.Take(10);
            SelectList sl = new SelectList((from s in _aa.Agent.ToList() select new { COUNTER = s.COUNTER, FullName = s.LASTNAME + ", " + s.FIRSTNAME }), "COUNTER", "FullName", null);
            ViewBag.Agent= sl;
            if (LamcomAgents != null && LamcomAgents != 0)
            {
                return View(modelAgent);                
            }
            return View(modelAgent);            
        }

適切な linq クエリはまだ httppost アクションに含まれていませんが、後で行います。ビューでエラーを発生させずに、最初の実行時に httpget の null 許容モデルを渡す方法を知りたいだけです。これは私がビューに持っているものです:

@model IEnumerable<Company.Agents>

@{
    ViewBag.Title = "AddNewAgent";
}

<h2>Add New Agent</h2>

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

    <fieldset>
        <p>Agents in System @Html.DropDownList("Agent", String.Empty)&nbsp;&nbsp; <input type="submit" name="Get Agent" value="Get Agent" title="Get Agent" id="btnGetAgent" /></p>
        <legend>Agents</legend>      
   <table>       
                    <tr>
                        <th>@Html.DisplayNameFor(model => model.A_FirstName)</th>
                        <th></th>
                    </tr>
                    <tr>

                    </tr>              
            @foreach(var item in Model)
            {
                <tr><td>
               @Html.DisplayFor(modelItem => item.A_FirstName)
                    </td></tr>
            }
                </table>

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

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

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

前もって感謝します、 ラツィアーレ

4

1 に答える 1

1

modelAgenttそれがタイプであると仮定してIEnumerable<Company.Agents>

modelAgentt代わりにビューに渡す必要があると思いますagg

したがって、コードを Get メソッドから変更します

return View(agg);

return View(modelAgentt);
于 2013-09-30T12:37:41.603 に答える