开发者

ASP.NET MVC How can parent view communicate to its partial view or vice versa

I have a strongly typed partial view that wraps a jQuery datetime picker, as follows:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<AkwiMemorial.Models.CalendarModel>" %>      
<script type="text/javascript">
    $(document).ready(function() {
    $('#datepicker').datepicker({
        inline: true,
        onSelect: function(dateText, inst) {
        $('#SelectedDate').val(dateText);
        $('form').trigger('submit');   }});
    });
</script>


 <% using (Html.BeginForm()) { %>   
 <div>
   <div id="datepicker"/>
   <%= Html.HiddenFor(x => x.SelectedDate)%>
</div>
 <% } %>

The model for this partial view contains a single property that will represent the current selected date on the calender. This model is defined as follows:

public class 开发者_如何转开发CalendarModel
    {
        public CalendarModel() { SelectedDate = DateTime.UtcNow; }        
        public DateTime SelectedDate
        {
            get;
            set;
        }
    }

The idea is to be able to re-use this calendar partial view throughout the rest of the web application. A main view that needs this calendar as a partial view will call it like this:

 <% Html.RenderAction("Calendar", "Calendar", ViewData["CalendarData"]); %>

Where the CalendarController is defined soley to service HttpPost request from the calender partial view. The CalendarController is defined as follows:

public class CalendarController : Controller
    {      
        string _parentControllerName;
        string _parentControllerCallback;        

        public ActionResult Calendar(string controllerName, string controllerCallback)
        {
            var model = new CalendarModel();
            _parentControllerCallback = controllerCallback;
            _parentControllerName = controllerName;
            return View(model);
        }

       [HttpPost]
        public ActionResult Calendar(CalendarModel model)
        {            
            //return View(model);
            return RedirectToAction("parentControllerCallback", "parentControllerName", model);
        }        
    }

I have a problem in that child actions are not allowed to redirect so my Calendar method with HttpPost attribute will fail. Nonetheless, I will like the selected data from this controller to be propagated somehow to the parent view's controller who then decides what to do with the selected date.

For example, the parent news page I have displays news and article summaries. This calendar patial view is rendered as part of the news page using RenderAction as posted above with CalendarData containing the NewsController name and the call back I want to invoke after calendar's selected data is changed. Once NewsController gets this selected date, it will grab all news articles for selected date and re-render its main content.

My main challenge now is how to get the calendar's selected data back to parent controller due to the redirect restriction. Also, I am hoping to use RouteValueDictionary to pass data from NewsController to CalendarController but need to read more on routing to see if this will work. Any insights into this will be great. I am also trying to follow good design pratices so if anyone has a completely better way to do this while maintaining re-usability of my calendar control, that would be even better.

Any suggestions


Looks like I am making this more complex that needed. I ditched the idea of using a CalendarController and instead used the parent view's controller to process the post back as follows:

        [HttpPost]
        public ActionResult Index(FormCollection form)
        {
            var date = DateTime.Today; 
            try
            {
               date = DateTime.Parse(form["DateStr"]);

            }
            catch 
            {                
            }

            return Refresh(date); 
        } 


If your calendar controller does what I think it does - you probably ought to convert it into an Html Helper so you can just use...

Html.CalendarFor(m => m.FromDate)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜