1
 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;
    }
}

我正在使用 ASP.net MVC 3。在我看来,这是一个ajax/javascript方法。

就语法而言,这是正确的吗?

familyMemberId是动态的,然而,userId不是。我想将userId我的 viewModel 传递给这个 ajax 调用,我该怎么做?

4

1 回答 1

1

您想要做的是从控制器中的模型中获取数据到视图中。这就是 MVC 的全部意义所在。来自MSDN MVC 4 教程

控制器:

您可以使用 VS 菜单系统和实体框架来定义您的模型,以便您实际访问数据库。

 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);
    }
    //...

看法:

@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;
        }
    }

这是因为,正如您所指出的,用户 ID 在页面加载后不是动态的。如果您想要真正的动态行为,则需要在 HTML 中创建一些其他钩子以供 javascript 抓取。

于 2013-03-08T19:31:50.660 回答