新的MVC帮助请 [英] New To MVC Help Please

查看:79
本文介绍了新的MVC帮助请的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将myfunction传递给GamesController





  function  myfunction(command,args,Name){
switch (command){
case CMD_Data
document .getElementById( HiddenField1)。value = args;
document .getElementById( HiddenField2).value =姓名;

// 想在此处添加?
断裂;
}

}







 命名空间 MyLogin.Controllers 
{
[授权]
public class GamesController:Controller
{
GamesContext gam = new GamesContext() ;

[HttpPost]
public ActionResult Gangster( string HiddenField1, string HiddenField2)
{
Games mygame = new Games();

if (ModelState.IsValid)
{
mygame.UserName = User.Identity.Name;
mygame.HiScore = Int32 .Parse(HiddenField1);
mygame.GameName = HiddenField2;
ViewBag.Message = 分数已保存;
gam.games.Add(mygame);
gam.SaveChanges();

return RedirectToAction( 歹徒);
};
return View();
}

解决方案

我认为你想要调用 GamesController的 <来自你的JS功能的code> Gangster 动作吧?

那么你有很多方法可以做到这一点。

< a href =http://stackoverflow.com/questions/14049817/in-asp-net-mvc-all-possible-ways-to-call-controller-action-method-from-a-razor#14050121> In ASP.NET MVC:从Razor视图调用Controller Action Method的所有可能方法



我无法确定但我认为你会想要类似于以下方法。

您可以使用包含所需帖子操作的表单,然后只需提交您的数据。

因此,您将获得以下内容:

 <  表单    id   =   myform    action  < span class =code-keyword> = 游戏/黑帮   方法  = 发布 >  
< 输入 类型 = 隐藏 id = HiddenField1 name = HiddenField1 value = < span class =code-keyword> / >
< 输入 type = hidden id = HiddenField2 名称 = HiddenField2 value = / >
< / form >



您只需在 myfunction 中添加以下内容:

  function  myfunction(command,args,Name){
// ...

document .getElementById( < span class =code-string> myform)。submit();
break ;
}
}



你也可以调整一下你的行动:

 [HttpPost] 
public ActionResult Gangster(FormCollection collection)
{
string HiddenField1 =集合[ HiddenField1];
string HiddenField2 = collection [ HiddenField2 ];

游戏mygame = 游戏();
// ...
}


how do i pass myfunction to GamesController


function myfunction(command, args, Name) {
    switch (command) {
        case "CMD_Data":
            document.getElementById("HiddenField1").value = args;
            document.getElementById("HiddenField2").value = Name;

            // want to add it here?
            break;
    }

}




namespace MyLogin.Controllers
{
    [Authorize]
    public class GamesController : Controller
    {
        GamesContext gam = new GamesContext();

        [HttpPost]
        public ActionResult Gangster(string HiddenField1, string HiddenField2)
        {
            Games mygame = new Games();

            if (ModelState.IsValid)
            {
                mygame.UserName = User.Identity.Name;
                mygame.HiScore = Int32.Parse(HiddenField1);
                mygame.GameName = HiddenField2;
                ViewBag.Message = "Score Saved";
                gam.games.Add(mygame);
                gam.SaveChanges();

                return RedirectToAction("Gangster");
            };
            return View();
        }

解决方案

I presume you want to call GamesController's Gangster action from your JS function, right?
Well there are quite a few ways you can do that.
In ASP.NET MVC: All possible ways to call Controller Action Method from a Razor View

I cannot be certain but I presume you would want something like the following approach.
You can use a form with the required post action and then just submit it with your data.
So for this you would have something like the following:

<form id="myform" action="Games/Gangster" method="post">
    <input type="hidden" id="HiddenField1" name="HiddenField1" value=""/>
    <input type="hidden" id="HiddenField2" name="HiddenField2" value=""/>
</form>


And you would just add the following in myfunction:

function myfunction(command, args, Name) {
    // ...

        document.getElementById("myform").submit();
        break;
    }
}


Also you can adjust your action a bit:

[HttpPost]
public ActionResult Gangster(FormCollection collection)
{
    string HiddenField1 = collection["HiddenField1"];
    string HiddenField2 = collection["HiddenField2"];
	
    Games mygame = new Games();
    // ...
}


这篇关于新的MVC帮助请的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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