RazorPage-Get、Post、自定义handler、ajax、partial

OnGet/Post{Handler}{Async}

@page
@model CoreMvc.PM1Model
@{
    ViewData["Title"] = "PM1";
}

<h1>PM1</h1>

<p>
    nj:   @Model.Age+@Model.name
</p>
<form>
    @Html.AntiForgeryToken()
    <p>
        name:<input asp-for="name" />
        <input type="submit" value="sumbit"  formmethod="post" />
    </p>
    <p>
        <button asp-page-handler="Demo1" formmethod="post">自定义Handler</button>
    </p>
    <p>
        <button onclick="ajax(); return false;">ajax</button>
    </p>
    <p>
        @await Html.PartialAsync("_CustomPartial", new PartialCus { Name = Model.name, Age = Model.Age, CurrTime = DateTime.Now })
    </p>

    <partial name="_CustomPartial" model="new PartialCus { Name = Model.name, Age = Model.Age, CurrTime = DateTime.Now }" />

    @{
        var partiCUs = new PartialCus { Name = Model.name, Age = Model.Age, CurrTime = DateTime.Now };
    }
    <partial name="_CustomPartial" for="@partiCUs" />
</form>
<script src="~/lib/jquery/dist/jquery.js"></script>
<script type="text/javascript">
    function ajax() {
        alert(3232);
        $.ajax({
            //请求方式
            type: "POST",
            //请求的媒体类型
            //contentType: "application/json;charset=UTF-8",
            //请求地址
            url: "?handler=ajax1",
            headers: {
                //.AddRazorPagesOptions(o=> o.Conventions.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute()));
                RequestVerificationToken:
                    $('input:hidden[name="__RequestVerificationToken"]').val()
            },
            dataType: "JSON",
            //数据,json字符串
            data: { name: "ajax-name", age: 10 },
            //请求成功
            success: function (result) {
                alert(result)
            },
            //请求失败,包含具体的错误信息
            error: function (e) {
                console.log(e.status);
                console.log(e.responseText);
            }
        });
    }
</script>

 

public class Person1
    {
        public int Age { get; set; }
        public string Name { get; set; }
        protected string Name2 { get; set; }
    }
    [BindProperties(SupportsGet =true)]
    public class PM1Model : PageModel
    {
         
        public int Age { get; set; }
        public Person1 Person { get; set; }
        //[FromForm]
        public string name { get; set; } 
        public void OnGet()
        {

        }

        public void OnPost()
        {

        }
        public void OnPostDemo1()
        {

        }
        public JsonResult OnPostAjax1()
        { 
            return new JsonResult(Person);
        }
    }

 

posted @ 2020-04-19 15:55  哈哈2222  阅读(742)  评论(0编辑  收藏  举报