简体   繁体   English

如何将ViewModel属性传递给ajax post方法?

[英]How can I pass a ViewModel property to an ajax post method?

 function ResendEmailInvite(internalUserId, familyMemberId) { 
       theinternalUserId = internalUserId;
       theFamilyMemberId = familyMemberId;
      if(confirm('Are you sure you want to resend this family member's invite?')){
         $.ajax({
             type: "POST",
             url:"/Admin/ResendFamilyMemberEmail",
             data: {internalUserId : theinternalUserId, familyMemberId : theFamilyMemberId}, 
             success: function(response){
                alert(response);
             },
             error: function(){
                alert("Error");
            }
        });
        return false;
    }
}

I am using ASP.net MVC 3. This is an ajax/javascript method in my view. 我正在使用ASP.net MVC3。在我看来,这是ajax / javascript方法。

As far as the syntax goes, is this correct? 就语法而言,这是正确的吗?

The familyMemberId is going to be dynamic, however, the userId is not. familyMemberId将是动态的,但是userId不是。 I want to pass the userId from my viewModel to this ajax call, how can I do this? 我想将userId从我的viewModel传递到此ajax调用,该怎么做?

What you're wanting to do is get the data from the model in your controller into the view. 您要做的是将控制器中模型的数据放入视图中。 This is what MVC is all about. 这就是MVC的全部意义。 From the MSDN MVC 4 Tutorial : MSDN MVC 4教程

Controller: 控制器:

You can define your model using the VS menu system and the Entity Framework so you're actually accessing the database. 您可以使用VS菜单系统和Entity Framework定义模型,以便实际访问数据库。

 public class YourController : Controller
{
     private YourDBContext db = new YourDBContext();

     public ActionResult YourAction(int user_id = 0)
    {
        User user = db.Users.find(user_id);
        if(user == null) {
           return HttpNotFound(); // Or unauthorized or whatever
        }
        return View(user);
    }
    //...

View: 视图:

@Model IEnumerable<MvcUser.Models.User>

<!-- other stuff -->

 <script type="text/javascript>
 // the rest of your script
        function ResendEmailInvite(internalUserId, familyMemberId) { 
           theinternalUserId = @Model.userId;
           theFamilyMemberId = familyMemberId;
          if(confirm('Are you sure you want to resend this family member's invite?')){
             $.ajax({
                 type: "POST",
                 url:"/Admin/ResendFamilyMemberEmail",
                 data: {internalUserId : theinternalUserId, familyMemberId : theFamilyMemberId}, 
                 success: function(response){
                    alert(response);
                 },
                 error: function(){
                    alert("Error");
                }
            });
            return false;
        }
    }

This works because, as you pointed out, the userId is not dynamic after the page has loaded. 如您所指出的,这之所以可行是因为页面加载后,userId不是动态的。 You would need to create some other hook in your HTML for javascript to grab if you wanted really dynamic behavior. 如果您想要真正的动态行为,则需要在HTML中创建一些其他的钩子以供JavaScript抓取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们如何为 ajax 调用传递 @odataid 以调用将所有者添加到组的 post 方法? - How can we pass @odataid for an ajax call to call a post method that adds an owner to group? 我想使用 ajax 在 post 方法中传递多个参数。 - I want to pass multiple parameters in post method using ajax. 如何将绑定到KoGrid单元格的项目传递给ViewModel - How Can I Pass The Item Bound To A KoGrid Cell To The ViewModel 如何通过 Ajax 将视图模型发送到 controller - How can I send a viewmodel to controller through Ajax 如何将此数据传递到表单提交调用(类似于ajax帖子的数据参数)? - How can I pass in this data into a form submit call (similar to data parameter for an ajax post)? 如何将数据从 ViewModel 发布到控制器方法中? - How to post data from ViewModel into a controller method? 如何将组件方法绑定到 ExtJS 中的 ViewModel? - How can I bind a component method to a ViewModel in ExtJS? WCF Ajax服务-如何将数组或JSON传递给服务? 方法参数类型应该是什么? - WCF Ajax service - How can I pass an Array or JSON to the service? What should the method parameter type be? 如何使用$ .ajax将变量传递给alert()? - How can I pass a variable to alert() with $.ajax? 如何将数组传递给ajax调用? - How can I pass an array into an ajax call?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM