简体   繁体   English

获取局部视图以加载数据

[英]Get Partial View to load Data

I have a controller post (Index POST) that returns a partial view (Modal). 我有一个控制器帖子(索引POST),它返回部分视图(模态)。 This partial view is to load data from a model that is returned from its respective Get ActionResult method. 此局部视图用于从模型加载数据,该模型是从其各自的Get ActionResult方法返回的。 It ignores this method. 它忽略此方法。 Is there a way to make it load data from the controller? 有没有办法使其从控制器加载数据?

    [HttpGet]
    public ActionResult Index()
    {

        return View();

    }

    [HttpPost]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public ActionResult Index(string date)
    {
        string[] dateSplit = date.Split(new char[] { 'T' });

        DateTime objDate = Convert.ToDateTime(dateSplit[0]);
        TempData["date"] = objDate;

        return PartialView("~/Views/Home/Modal.cshtml");
    }

This is the partial View's Get Method, which doesn't load when the Index post returns the partial view. 这是局部视图的Get方法,当Index帖子返回局部视图时,该方法不会加载。

[HttpGet]
public ActionResult Modal()
{
    var content = db.Calendars.ToList();
    if(TempData["date"] != null)
    {
        DateTime objDate = Convert.ToDateTime(TempData["date"]); 
        var contentDate = db.Calendars.Where(x => x.startDate == objDate).ToList();

        return PartialView(contentDate);
    }


    return PartialView(content);



}

Here is the View: 这是视图:

@model IEnumerable<webby.Models.Calendar>



<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="padding:20.5% 15%;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="myModalLabel">Events on @ViewBag.Date</h4>
            </div>
            <div class="modal-body">
                <table>
                    @foreach(var item in Model)
                    { 
                    <tr>
                        <td>
                            @Html.DisplayFor(model => item.events)
                        </td>
                    </tr>
                    <tr>
                        <td>
                            @Html.DisplayFor(model => item.type)
                        </td>
                    </tr>
                    <tr>
                        <td>
                            @Html.DisplayFor(model => item.content)
                        </td>
                    </tr>
                    }


                </table>


            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Save changes</button>
            </div>
        </div>
    </div>
</div>

<script>
    $('#myModal').modal();
</script>

After running the application and once the post returns the partial view, the partial view catches a null error. 运行应用程序后,一旦帖子返回部分视图,部分视图将捕获空错误。

return PartialView("~/Views/Home/Modal.cshtml"); 返回PartialView(“〜/ Views / Home / Modal.cshtml”);

The above line is not going to call your Modal() action. 上面的行不会调用您的Modal()操作。 It will simply try to return the View (cshtml). 它将仅尝试返回视图(cshtml)。 (And in this case you are not giving the view a model.) (在这种情况下,您没有给视图提供模型。)

You might pull off your goal by having the last line of Index (post): 您可以通过拥有Index(发布)的最后一行来实现自己的目标:

return RedirectToAction("Modal");

(That said, I'm a bit unclear on exactly what you're trying to accomplish, so a more detailed description might help get to a complete solution.) (也就是说,我对您要完成的目标尚不清楚,因此,进行更详细的描述可能有助于获得完整的解决方案。)

Also, I don't know exactly what the ScriptMethod attribute is, but I'm pretty sure you don't want it decorating your post action. 另外,我不完全知道ScriptMethod属性是什么,但是我很确定您不希望它装饰您的post操作。

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

You cannot have modal actions. 您不能有模式操作。 When you call 你打电话时

return PartialView("~/Views/Home/Modal.cshtml");

it is going to simply open the view with whatever you pass into it (which as written is absolutely nothing). 它将使用您传递给视图的任何内容简单地打开视图(按书面说明,绝对没有任何内容)。

What you want to do is put the code: 您想要做的就是输入代码:

var content = db.Calendars.ToList();
if(TempData["date"] != null)
{
    DateTime objDate = Convert.ToDateTime(TempData["date"]); 
    content = db.Calendars.Where(x => x.startDate == objDate).ToList();
}

inside your HttpPost Index action and pass the model to the modal as such: 在HttpPost Index操作中,然后将模型传递给模式:

return PartialView("~/Views/Home/Modal.cshtml", content);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM