Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.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
C# 在一个按钮asp.net mvc中同时调用c和javascript_C#_Jquery_Asp.net_Ajax_Model View Controller - Fatal编程技术网

C# 在一个按钮asp.net mvc中同时调用c和javascript

C# 在一个按钮asp.net mvc中同时调用c和javascript,c#,jquery,asp.net,ajax,model-view-controller,C#,Jquery,Asp.net,Ajax,Model View Controller,我在ASP.NET MVC视图中有一个按钮。我发现我们实际上可以通过在视图前添加@{}来在视图中使用c命令。所以我想知道,我的按钮如何调用javascript函数并将数据添加到该视图中随模型传递的列表中。 这是我的模型 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; using Syst

我在ASP.NET MVC视图中有一个按钮。我发现我们实际上可以通过在视图前添加@{}来在视图中使用c命令。所以我想知道,我的按钮如何调用javascript函数并将数据添加到该视图中随模型传递的列表中。 这是我的模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Net.Mail;
using System.Web.Mvc;

namespace Dutch_Lady_Mailling_App.Models
{
    public class SendMailModel
    {
        [DataType(DataType.EmailAddress), Display(Name = "To")]
        [Required(AllowEmptyStrings = false)]
        public string ToEmail { get; set; }
        [Display(Name = "Body")]
        [DataType(DataType.MultilineText)]

        public string EMailBody { get; set; }
        [Display(Name = "Subject")]
        public string EmailSubject { get; set; }

        [Display(Name = "CC")]
        public string EmailCC { get; set; }
        [Display(Name = "BCC")]
        public string EmailBCC { get; set; }

        public string time { get; set; }
        public string resourceFile{get;set;}
        public string Data { get; set; }
        public bool sendtemplate { get; set; }
        public bool senddis { get; set; }
        public List<Contact> contacts { get; set; }
        public List<Distributor> distributors { get; set; }
        public string SelectedDis { get; set; }

        public string SelectedTemplate { get; set; }
        public IEnumerable<SelectListItem> Templates { get; set; }

    }

}

在我看来,单击按钮会将所选联系人添加到列表中,还可以通过调用jquery函数将所选元素移动到另一个div,而无需重新加载页面,这正是我想要的。据我所知,您需要执行两个操作,一个是服务器端,另一个是客户端。当然,如果你调用一个服务器端方法,你会得到一个post back,你所有的页面都会被重新发送给你,所以你可以用服务器端方法完成你所有的工作,因为页面会被再次创建——我的意思是不需要javascript函数。但是,防止这种情况的方法,以及针对您的场景的最佳方法是使用ajax更新列表。最简单的方法是使用jQueryAjax方法,如$.ajax或其简化版本$.post或$.get,具体取决于您的场景和向服务器发送信息的方式。例如:

$('#your_button_id').click(function(){
    $.post( "manage_contacts_api_url", "selected_contacts_ids", function( data ) {
        // update your div here
    });
});

作为目标方法的JS函数和控制器操作在哪里?因为您声明调用jquery函数而不重新加载页面,所以我确信您需要绑定AJAX回调来单击按钮的事件。