可以使用按钮通过索引视图更新数据吗?

时间:2012-04-12 15:39:08

标签: asp.net-mvc asp.net-mvc-3 model calendar

我正在将我的思维方式和编程从Webforms转换为MVC3。为了记录,它伤害了我的头脑。

所以我正在创建一个具有月视图的日历(页面仅显示该月的日期和事件)。我把两个按钮上一个和下一个按钮从一个月到另一个月。在Webforms中,任何更改都会发生在Calendar.aspx上。但是, 似乎 我必须制作两个ActionResults(都是HttpPost)。一个进入Previous视图,另一个进入Next视图。只是为了得到同样的效果。我希望只更新Index ActionResult / View上的日历,但我不知道该怎么做。我的索引视图如下。

长话短说,有没有办法更新日历并返回索引视图?或者,当我点击相应的按钮(上一页和下一页)时,我是否必须制作另外两个视图(上一页和下一页)来显示新月?

@model HTMLMVCCalendar.Models.MonthModel

@{
    ViewBag.Title = "Home Page";
    int month = Model.Month;
    int year = Model.Year;
    int numberOfDays = DateTime.DaysInMonth(year, month);
    int startDay = (int)(Convert.ToDateTime(month + "/1/" + year).DayOfWeek);
    int startCount = 1;
}

<h2>@ViewBag.Message</h2>
<p>
    To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>
<div>  
    <table id="calendar">
        <thead>
            <tr>
                <th id="monthHeader" colspan="7">
                    <h3>
                    @{
                        @Convert.ToDateTime(month + "/1/" + year).ToString("MMMM");
                    }
                    </h3>
                </th>
            </tr>
            <tr>
                <th>Sun</th>
                <th>Mon</th>
                <th>Tues</th>
                <th>Wed</th>
                <th>Thur</th>
                <th>Fri</th>
                <th>Sat</th>
            </tr>
        </thead>
        <tbody>
            <tr>
            @for (int i = 0; i < startDay; ++i)
            {
                @:<td><div><span>&nbsp;</span></div><div><span>&nbsp;</span></div></td>
            }
            @for (int j = startDay; j < ((numberOfDays + startDay) + 1); ++j)
            {
                <td>
                    <div><span>@startCount</span></div>
                    <div>
                        <span>
                            @{
                                var todaysEvents = Model.AllDays.ToList().FindAll(d => d.CalDate.Day == startCount);
                                foreach(HTMLMVCCalendar.Models.CalendarModel eventsToday in todaysEvents)
                                {
                                    foreach(HTMLMVCCalendar.Models.EventModel eventToday in eventsToday.CalEvents)
                                    {
                                        <text>
                                            &nbsp;@eventToday.DayCode:<br />
                                            &nbsp;@eventToday.Subject:<br />
                                            &nbsp;@eventToday.EventDesc<br /><br />
                                        </text>
                                    }                                 
                                }
                            }
                        </span>
                    </div>
                </td>
                            if ((j + 1) % 7 == 0)
                            {
                    @:</tr><tr>
                }
                ++startCount;
            }
            </tr>
        </tbody>
    </table>
    <div>
        <input id="previous" type="submit" value="submit" />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input id="next" type="submit" value="submit" />
    </div> 
</div>

更新 我试过这个,我认为它应该有效。不知道为什么没有?

    <div>
        @using (Html.BeginForm("Previous", "Home"))
        {
            <input id="previous" type="submit" value="Previous" />
        }
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        @using (Html.BeginForm("Next", "Home"))
        {
            <input id="next" type="submit" value="Next" />
        }
    </div> 


public ActionResult Index()
{
    ViewBag.Message = "Welcome to ASP.NET MVC!";

    int month;
    int year;

    int.TryParse(ViewBag.Year, out year);
    int.TryParse(ViewBag.Month, out month);
    Calendar monthEventsCal = new Calendar();

    HTMLMVCCalendar.Models.MonthModel allMonthEvents = monthEventsCal.monthEvents(year, month);
    return View("Index", allMonthEvents);            
}

[HttpPost]
public ActionResult Previous(HTMLMVCCalendar.Models.MonthModel prevMonth)
{
    Calendar monthEventsCal = new Calendar();
    var newMonth = monthEventsCal.previousMonth(prevMonth.Year, prevMonth.Month);

    ViewBag.Month = newMonth.Item2;
    ViewBag.Year = newMonth.Item1;
    return RedirectToAction("Index");
}

[HttpPost]
public ActionResult Next(HTMLMVCCalendar.Models.MonthModel nextMonth)
{
    Calendar monthEventsCal = new Calendar();
    var newMonth = monthEventsCal.nextMonth(nextMonth.Year, nextMonth.Month);

    ViewBag.Month = newMonth.Item2;
    ViewBag.Year = newMonth.Item1;
    return RedirectToAction("Index");
}

2 个答案:

答案 0 :(得分:1)

建议的可行方法

public virtual ActionResult Index(int? month)
{
    int relativeMonth = 0;
    if(month.HasValue)
        relativeMonth = month.Value;

    //Create ViewModel fetching calender data using relativeMonth value.
    //For example if relativeMonth is 0 fetch current month view model.
    //If relative month is -3 fetch calendar data from three months ago.

    if(Request..IsAjaxRequest()) //Optional. If you want ajax.
        return PartialView("Calendar", model);

    return View(model);
}

使用此方法,您的下一个和上一个按钮现在是以下链接,其中Previous将链接到relativeMonth - 1,next接下来是relativeMonth + 1.我不认为发布它们是必要的,因为您实际上并未提交任何表单数据(使用这种方法,我会将下一个和上一个链接设置为按钮样式)。

如果你想启用ajax,我会将你的日历视图放在一个单独的cshtml中,并在index.cshtml中将其称为部分视图。

答案 1 :(得分:0)

有几种方法可以做到这一点,最简单的方法是执行ajax调用并使用返回的数据更新视图 http://bob-the-janitor.blogspot.com/2010/12/easy-ajax-with-aspnet-mvc-and-jquery.html 如果你想获得幻想,你甚至可以从ajax调用返回一个视图,并替换你当前的视图,如下所示 http://bob-the-janitor.blogspot.com/2011/11/more-ajax-with-mvc-using-partial-views.html