简体   繁体   English

使用参数MVC Razor在一页中重新加载部分视图

[英]Reload some partial view in one page with parameter MVC Razor

I'm a student and also new in ASP.NET programming. 我是学生,也是ASP.NET编程的新手。 I'm using Razor MVC view engine. 我正在使用Razor MVC视图引擎。 I want to display some partial view in one page (Index.cshtml) using two datetime parameters (startDate & endDate) and with one button as trigger. 我想使用两个datetime参数(startDate和endDate)并以一个按钮作为触发器在一页(Index.cshtml)中显示某些局部视图。 But I don't know how to pass the parameters to the Controller and then make the page reload with information between startDate & endDate. 但是我不知道如何将参数传递给Controller,然后使用startDate和endDate之间的信息使页面重新加载。

Index.cshtml (Updated) Index.cshtml (已更新)

 @model LoginProject.Models.Date @{ ViewBag.Title = "Welcome Maestro"; } <!-- Bootstrap CSS and bootstrap datepicker CSS used for styling the demo pages--> <link rel="stylesheet" href="~/Content/themes/css/datepicker.css"> <link rel="stylesheet" href="~/Content/themes/css/bootstrap.css"> <div class="well"> <table class="table"> <thead> <tr> <div id="result"></div> @using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "result", HttpMethod = "POST", InsertionMode = InsertionMode.Replace })) { <th>@Html.EditorFor(Model => Model.start)</th> <th>@Html.EditorFor(Model => Model.end)</th> <td><input type="submit" value="Cek" class="btn btn-primary" id="sub" /></td> } </tr> </thead> </table> </div> <!-- Load jQuery and bootstrap datepicker scripts --> <script src="~/Scripts/js/jquery-1.9.1.min.js"></script> <script src="~/Scripts/js/bootstrap-datepicker.js"></script> <script> $(function () { // disabling dates var nowTemp = new Date(); var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0); var st = $('#start').datepicker({ onRender: function (date) { return date.valueOf(); } }).on('changeDate', function (ev) { if (ev.date.valueOf() > en.date.valueOf()) { var newDate = new Date(ev.date) newDate.setDate(newDate.getDate() + 1); en.setValue(newDate); } st.hide(); $('#end')[0].focus(); }).data('datepicker'); var en = $('#end').datepicker({ onRender: function (date) { return date.valueOf() <= st.date.valueOf() ; } }).on('changeDate', function (ev) { en.hide(); }).data('datepicker'); }); </script> @{Html.RenderAction("baruLama", "Home");} @{Html.RenderAction("kunjunganPoli", "Home");} 

HomeController.cs : HomeController.cs

 using LoginProject.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace LoginProject.Controllers { public class HomeController : Controller { private rsudwEntities5 db = new rsudwEntities5(); public ActionResult Index() { return View(); } public ActionResult baruLama(Date date) { if (date.start != null && date.end != null) { return PartialView("_totalKunjunganGender", db.getTotalKunjunganGender(date.start, date.end).ToList()); } else { var today = DateTime.Today; var month = new DateTime(today.Year, today.Month, 1); var first = month.AddMonths(-1); var last = month.AddDays(-1); return PartialView("_totalKunjunganGender", db.getTotalKunjunganGender(first, last).ToList()); } } public ActionResult kunjunganPoli(Date date) { if (date.start != null && date.end != null) { return PartialView("_totalKunjunganPoli", db.getKunjunganPoli(date.start, date.end).ToList()); } else { var today = DateTime.Today; var month = new DateTime(today.Year, today.Month, 1); var first = month.AddMonths(-1); var last = month.AddDays(-1); return PartialView("_totalKunjunganPoli", db.getKunjunganPoli(first, last).ToList()); } } } } 

Date.cs : Date.cs

 using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace LoginProject.Models { public class Date { public DateTime start = DateTime.Parse("01/08/2014"); //set default to 1 august 2014 public DateTime end = DateTime.Parse("01/09/2014"); //set default to 1 september 2014 public DateTime m { get { return start; } set { start = value; } } public DateTime s { get { return end; } set { end = value; } } } } 

To achieve that purpose what should I do? 为了达到这个目的,我该怎么办?

Should I use Ajax? 我应该使用Ajax吗?

Or Html.BeginForm? 还是Html.BeginForm?

But I can't use them to render multiple partial views. 但是我不能用它们来渲染多个局部视图。

There's multiple ways you could do this, this is just one of them. 您可以通过多种方式来执行此操作,这只是其中之一。

JQuery has a nice Load function you could use, each time you call it, it will refresh the partial view. jQuery具有很好的Load函数,您可以使用它,每次调用它都会刷新局部视图。 In this scenario, BeginForm is not necessary. 在这种情况下, BeginForm是不必要的。

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new  { id = "MyForm" }))
{

    @Html.Kendo().DatePicker().Name("RefDate").Value("09/25/2014")            


    <input type="button" value="Load Data" id="MyBTN" />    
    <br/>   


    <div id="MyPartial">
    </div>                 
}


<script>

    $("#MyBTN").click(function () {

        var date = $("#RefDate").val();
        var int = 5;

        $("#MyPartial").load('Home/ViewPartial/?refDate=' + date + '&days=' + int);        
    });


</script>

MyPartial div will be the placeholder for your partial view that you will call. MyPartial div将是您要调用的局部视图的占位符。 Your controller action would look something like this 您的控制器动作看起来像这样

    public PartialViewResult ViewPartial(DateTime refDate, int days)
    {                       
         MyModel model = new MyModel();
         //do stuff
         return PartialView("_MyPartial", model);
    } 

The parameters values in the query string above must match the parameters that the controller expects. 上面查询字符串中的参数值必须与控制器期望的参数匹配。

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

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