【问题标题】:mvc4 razor return empty model on httpgetmvc4 razor 在 httpget 上返回空模型
【发布时间】:2013-09-30 11:46:13
【问题描述】:

我想在 mvc4 razor 视图中返回 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 传递可空模型而不会在视图中出现错误。 这就是我的观点:

@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")
}

提前致谢,Laziale

【问题讨论】:

  • 什么是agg,它在哪里定义?向我们展示它的代码

标签: asp.net-mvc linq asp.net-mvc-4 razor model


【解决方案1】:

假设modelAgentt 的类型为IEnumerable&lt;Company.Agents&gt;

我认为您需要通过modelAgentt 而不是agg 来查看

所以从 Get Method 中更改您的代码

return View(agg);

return View(modelAgentt);

【讨论】:

    猜你喜欢
    • 2014-04-09
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 2015-03-14
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多