Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc Ajax.BeginForm、用户控件和更新内容_Asp.net Mvc - Fatal编程技术网

Asp.net mvc Ajax.BeginForm、用户控件和更新内容

Asp.net mvc Ajax.BeginForm、用户控件和更新内容,asp.net-mvc,Asp.net Mvc,我对用户控件有点问题。基本上,我想实现以下目标: [AcceptVerbs(HttpVerbs.Post), ValidateAntiForgeryToken] public ActionResult Create(InvoiceLine line) { if (Request.IsAjaxRequest()) { if (!ModelState.IsValid) { return PartialView("CreateLineControl", product)

我对用户控件有点问题。基本上,我想实现以下目标:

[AcceptVerbs(HttpVerbs.Post), ValidateAntiForgeryToken]
public ActionResult Create(InvoiceLine line)
{
if (Request.IsAjaxRequest())
{
    if (!ModelState.IsValid)
    {
        return PartialView("CreateLineControl", product);
    }
}           
return PartialView("DisplayLinesControl", product);
}
  • 我有一个编辑发票的视图
  • 在这个视图中,我有一个带有发票项列表的usercontrol
  • 我还有一个div,它通过jQuery激活,用于添加新的发票项
  • 添加发票项目时,我只想用项目列表刷新用户控件
  • 没有黑客我怎么做?我想到的是:

    [AcceptVerbs(HttpVerbs.Post), ValidateAntiForgeryToken]
    public ActionResult Create(InvoiceLine line)
    {
    if (Request.IsAjaxRequest())
    {
        if (!ModelState.IsValid)
        {
            return PartialView("CreateLineControl", product);
        }
    }           
    return PartialView("DisplayLinesControl", product);
    }
    

    首先,您将调用一个带有jQuery的aspx页面(我们将使用一个http处理程序,我们将在下面的web.config中映射该处理程序,稍后将对此进行详细介绍)

    基本思想是,我们希望服务器端将用户控件呈现为xhtml,并在我们的成功方法(客户端)中将这个“更新的”标记转储回DOM

    下面的技术是我用来通过HttpHandler利用用户控件重用控件的技术 对于ajax和.net工作

    下面的内容是通过w/.NET1.1完成的(但我相信您可以在.NET2.0+中完成) 下面的类实现了IHttpHandler,真正的工作在processrequest子部分,如下面所示

    当时我对此的唯一问题是asp.net控件不会在用户中呈现w/out表单标记 控件,所以我使用了普通的html,一切都很好

    Public Class AJAXHandler
    
        Implements IHttpHandler
    
        Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
            Get
                Return False
            End Get
        End Property
    
        Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
            Dim Request As System.Web.HttpRequest = context.Request
            Dim path As String = Request.ApplicationPath
            Dim newHtml As String
            Dim pg As New System.Web.UI.Page
    
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache)
            context.Response.ContentType = "text/html"
    
                                    Dim uc As New UserDetail
                                    uc = CType(pg.UserControl(path + "/Controls/UserDetail.ascx"), UserDetail)
                                    Dim sb As New System.Text.StringBuilder
                                    Dim sw As New System.IO.StringWriter(sb)
                                    Dim htmlTW As New HtmlTextWriter(sw)
    
                                    uc.LoadPage(custid, CType(pro, Integer))
                                    uc.RenderControl(htmlTW)
                                    context.Response.Write(sb.ToString())
                                    context.Response.End()
    
        End Sub
    
    
    End Class
    
    最后,在web.config中,您需要定义处理程序并将其映射到ajax调用中列出的aspx路径

      <system.web>
        <httpHandlers>
            <add verb="*" path="UserDetails.aspx" type="project.AJAXHandler, project" />
        </httpHandlers>
      </system.web>
    
    现在,在我的ascx中,我只需呈现html,这就是返回到浏览器的container.innerHTML工作(同样,在这种情况下,MVC和Webforms的客户端代码是相同的)

    
    用户名
    

    在MVC中使用更少代码的原因是,我们不必处理webforms中普通aspx文件所需的页面生命周期。

    这是一个非常好的解决方案。我还没有时间玩它。我会就此回复你的。时间过得真快:)我相信您不需要处理程序,因为控制器方法是公共的,可以通过适当的URL访问。但这是一个正确的方向
    Function Edit(ByVal id As Integer) As ActionResult
        Dim User As User = UserService.GetUserById(id)
    
        Return PartialView("User", User)
    End Function
    
    <%@ Control Language="VB" Inherits="System.Web.Mvc.ViewUserControl(Of User)" %>
    <% Dim User As User = DirectCast(Model, User)%>
    <div id="innerDetail">
        <label for='username'>Username</label>
        <input type="text" id='username' name='username' value="<%= User.Username %>" /><br />
    
        <a id='lnkUpdate' href='/Application/User.aspx/Edit/<%= User.ID %>' onclick='UpdateUser(this); return false;'>Update User Information</a>
        <span id='lblUpdateStatus' style='display: inline;'></span>
        </div>
    </div>