ASP.Net模型绑定-WebForms [英] ASP.Net Model binding - WebForms

查看:93
本文介绍了ASP.Net模型绑定-WebForms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求从事ASP.Net WebForms项目,我想像在ASP.Net MVC中一样使用ModelBinding.

I have been asked to work on an ASP.Net WebForms project and I want to use ModelBinding like I do in ASP.Net MVC.

我在使它起作用时遇到一些问题,如果可能,希望获得帮助.

I am having some problems in getting this to work and would like some help if possible.

如果我想将模型绑定到Form上的对象,则需要在FormView,GridView等中执行此操作,但是在这种情况下,我想使用FormView.

If I want to bind a model to objects on a Form I need to do this in a FormView, GridView and so on but in this instance I want to use a FormView.

下面的代码是我正在使用的简化版本,但每个方面都是相关的.

The below code is a simplified version of what I am working with but everybit relevant.

<asp:FormView runat="server" ID="frmMain" ItemType="WebFormsModelBinding.TestModel" SelectMethod="TestBinding_select" >
    <ItemTemplate>
        <asp:TextBox runat="server" ID="txtName" Text="<%#: BindItem.Name %>" />
        <br/>
        <asp:TextBox runat="server" id="txtAge" Text=" <%#: BindItem.Age %>" />

    </ItemTemplate>

</asp:FormView>
<asp:Button runat="server" id="bttnInsert" Text="button"/>

我有一个试图将数据传递回模型的JQuery脚本,但我不断收到内部500错误

I have a JQuery script that is attempting to pass the data back to the model but I keep getting a Internal 500 Error

$(document).ready(function() {

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


                var params = {
                    Name: "Dave",
                    Age: 55
                }

                $.ajax({
                    type: "POST",
                    url: "default.aspx/TestBinding",
                    data: JSON.stringify(params),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    //success: function (data) {
                    success: function () {
                        alert("it worked");
                        return false;

                    },
                    error: function (data) {

                        console.log(data); 
                        alert("didnt work");
                    }
                });
                return false;
            });
    })

这应该在默认页面中点击此代码

This should hit this code in the default page

    public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [System.Web.Services.WebMethod]
    public static bool TestBinding(TestModel model)
    {

        return true;
    }

    public TestModel TestBinding_select()
    {
        var model = new TestModel
        {
            Name = "Simon",
            Age = 33
        };

        return model;
    }
}

public class TestModel
{
    public string Name { get; set; }
    public int Age { get; set; }

}

我遇到的问题是,由于我无法使用ActionResult,因此必须声明不同的对象类型作为返回类型.在此示例中,我选择了布尔型,并且没有特定的原因.

The problems I am having is that as I cant use an ActionResult I have to declare a different object type as the return type. For this example I have chosen a bool and there is no specific reason for this.

为了创建WebMethod,我必须将方法设为静态.

In order to hit a WebMethod I have to make the method static.

我得到的错误是

无效的Web服务调用,缺少参数'model'的值."

"Invalid web service call, missing value for parameter: 'model'."

因此,在这种情况下,我可以将模型传递给UI并在FormView中呈现,但是我无法通过JQuery或回发传递模型.

So, I can pass a model to the UI and present it in the FormView in this instance, but I cannot pass a model back via JQuery or a postback.

是否缺少任何内容,或者真的必须将每个变量传递回然后将值传递给类,从而使模型绑定在WebForms中几乎没有意义

Is there anything that I am missing, or do I really have to pass each variable back and then pass the values to the class thus making model binding almost pointless in WebForms

推荐答案

如果您打算使用WebForms,则应该习惯使用PostBack模型,否则您将一直处于战斗状态针对所选框架.

If you ware going to work with WebForms, you should get used to working with the PostBack model, or you're going to be constanty fighting against the selected framework.

但是对于眼前的问题,请尝试以下操作:

But for the question at hand, try the following:

data: JSON.stringify({'model':params})

这篇关于ASP.Net模型绑定-WebForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆