iT邦幫忙

0

AJAX問題請教

https://ithelp.ithome.com.tw/questions/10194134

這是這是先前問的問題,大略上已經完成,後來我再追加查詢及上一個月下一個月功能

https://ithelp.ithome.com.tw/upload/images/20190702/20110132uv9yDn18Cr.jpg

查詢功能我已經完成,剩下上下個月的問題
目前我已經把需要的資料都準備好,就剩下回傳給畫面
這部份想請問該如何實現?
查詢的html

<div class="row">
    <div class="col-md-12 ca">
        @using (Html.BeginForm("DrScheduleList", "DrSchedule", new { area = "Center" }, FormMethod.Post, new { id = "queryForm", @class = "form-inline" }))
        {
            <div class="box box-primary">
                <div class="box-body form-flex-container">
                    <div class="col-inner-wrapper">
                        <div class="row-flex">
                            <div class="col-input">
                                @Html.CTextBoxFor(m => m.YeartoString, hStatus, new { @class = "form-control" })
                            </div>
                            <div class="col-label">
                                <label class="control-label">年</label>
                            </div>
                        </div>
                        <div class="row-flex">
                            <div class="col-input">
                                @Html.CTextBoxFor(m => m.MonthtoString, hStatus, new { @class = "form-control" })
                            </div>
                            <div class="col-label">
                                <label class="control-label">月</label>
                            </div>
                        </div>
                        <button type="button" class="btn btn-default pull-right  next">下個月</button>
                        <button type="button" class="btn btn-default pull-right  previous">上個月</button>
                        <button type="submit" class="btn btn-primary pull-right">查詢</button>

                    </div><!--form-flex-container-->
                </div><!-- /.box-body -->
            </div><!-- /.box -->
        }<!-- /BeginForm -->
    </div>
    <!-- /.col -->
</div>

ajax


        //取得textbox年分
        var textboxyear = $("#YeartoString").val();
        //取得textbox月分
        var textboxmonth = $("#MonthtoString").val();


        var previoustomonth = textboxmonth - 1;
        //如果月份等於0則變成去年12月
        if (previoustomonth != 0) {
            var previousmonth = $("#MonthtoString").val(previoustomonth);
        } else if (previoustomonth == 0) {
            var previousmonth = $("#MonthtoString").val("12");
            var previoustoyear = textboxyear - 1;
            var previousyear = $("#YeartoString").val(previoustoyear);
        }

        var year = $("#YeartoString").val();
        var month = $("#MonthtoString").val();
       
        
        $.ajax({
            async: false,
            cache: false,
            type: "POST",
            url: "@(Url.Action("PreviousMonth", "DrSchedule", new { area = "Center" }))",
            dataType: "json",
            data: {             
                previousyear: year,
                previousmonth: month,
            },
            success: function (data) {

            },
            error: function () {
                alert('error');
            }
        });

    })

controller

  public ActionResult PreviousMonth(string previousyear, string previousmonth, DrScheduleQueryViewModel model)
        {
            UserInfoSession user = SessionUserInfo();
            DrScheduleService svc = new DrScheduleService();
            //醫師名單
            model.DrEmpList = svc.FindDrScheduleByMonth(user.CID, "");
            //已安排名單
            model.DrEmp = svc.FindpreDrsch(user.CID, previousyear, previousmonth);
            //產生畫面
            model.result = model.getMonthArray(previousyear, previousmonth, model.DrEmp);
            return View(model);
        }
看更多先前的討論...收起先前的討論...
dragonH iT邦超人 5 級 ‧ 2019-07-02 11:34:45 檢舉
取得所查詢的月份 -> 畫出相對應的表格 -> 帶出相對應的資料

跟你之前畫單月的邏輯一樣
YoChen iT邦研究生 1 級 ‧ 2019-07-02 12:02:41 檢舉
以這樣的寫法,如果我沒記錯,
在Ajax Success以後的Data,
你應該會得到新的頁面的Html,
必須把舊的HTML蓋掉然後重新把js function bind上去,
可能會有點麻煩,
通常比較建議回傳Json Result,再針對Json做處理可能會簡單一些
不過我個人是比較推薦你可以把Year跟Month參數直接寫進原本的Action裡面,按按鈕的時候重新執行Action然後順便帶入參數就好了~
tenno081 iT邦研究生 4 級 ‧ 2019-07-02 13:08:09 檢舉
感謝,我把button屬性換成submit後真的就可以了

AJAX我再練習看看,謝謝
froce iT邦大師 1 級 ‧ 2019-07-02 14:06:49 檢舉
怪事,都會畫單月了,怎麼不會把他函式化,弄成
function renderMonth(year, month):

這樣子你愛畫幾月、想找幾月都行。

另外查前一個月、後一個月,有個函式庫很方便叫moment.js
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答