Asp.Net MVC3-复杂类未在[HttpPost]方法上作为null传递 [英] Asp.Net MVC3 - Complex class not being passed as null on [HttpPost] method

查看:104
本文介绍了Asp.Net MVC3-复杂类未在[HttpPost]方法上作为null传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我...再来一次!

我上了这些课,

 public class PrankTargetArgumentViewModel
{
    public PrankTarget Target { get; set; }
    public PrankDefinition Prank { get; set; }
    public List<PrankArgument> Arguments { get; set; }
}

public class PrankArgument
{
    public string Name { get; set; }
    public string Value { get; set; }
}

我正在做的是-如果当前的ParkDefinition需要参数,则他们在保存到另一个应处理参数集合的Action上执行ActionRedirect

and what I'm doing is - if this current ParkDefinition needs arguments them im doing an ActionRedirect on the save to another Action which should handle the gathering of the Arguments

我的操作结果是这样的.

My Action result is like this..

    public ActionResult PrankArguments()
    {
        PrankInstance currentInstance = SessionContext.CurrentPrankInstance;

        if (currentInstance == null)
            throw new ArgumentNullException("currentInstance");

        PrankTargetArgumentViewModel model = new PrankTargetArgumentViewModel();
        model.Prank = currentInstance.Prank;
        model.Target = currentInstance.Target;

        string[] args = model.Prank.Arguments.Split('|');

        model.Arguments = new List<PrankArgument>();
        foreach (string s in args)
        {
            model.Arguments.Add(new PrankArgument { Name = s, Value = s });
        }

        return View(model);
    }

我的http post方法只是一个带有PrankTargetArgumentViewModel参数的空方法

my http post method is just an empty method with the parameter of PrankTargetArgumentViewModel

    [HttpPost]
    public ActionResult PrankArguments(PrankTargetArgumentViewModel model)
    { 
        return View();
    }

我的HTML就是这样.

My HTML is like this..

@using (Html.BeginForm())
{
@Html.EditorFor(x => Model)  

<p>
    <input type="submit" value="Create" />
</p>
}

所以我的问题是,在 PrankArguments(PrankTargetArgumentViewModel模型)后发操作上,模型参数始终为null.后面的帖子中会有我添加的新参数.

So my problem is this, on the PrankArguments(PrankTargetArgumentViewModel model) post back action, the model param is always null.. I've filled the object with values on the load so I guessed they would be there on the post back with the new arguments that I added.

所以流程是这样的.

创建恶作剧 如果恶作剧需要参数,则加载 ActionResult PrankArguments() 向已经填充的对象添加额外的参数. 保存,调用 ActionResult PrankArguments(PrankTargetArgumentViewModel模型) -这就是问题所在,模型参数作为null传回.

Create Prank If prank needs arguments then load ActionResult PrankArguments() Add extra arguments to an already poplulated object. save, Call ActionResult PrankArguments(PrankTargetArgumentViewModel model) -- this is where the problem is, the model parameter is passed back as null.

这个问题我已经发生过好几次了,并且总是放弃了,但是这次我不会让它发生的!

Ive had this problem quite a few times and always just given up but im not going to let that happen this time!

任何帮助都会很棒!干杯,Ste!

any help would be great! cheers, Ste!

Ps.如果您需要我的代码了,请告诉我.

Ps. If you need anymore of my code just let me know.

编辑-删除了视图包调试属性!

推荐答案

在查看了我自己的代码之后-我注意到我不需要整个PrankTargetArgumentViewModel,并且一个简单的Arguments列表就可以了.

After reviewing my own code - I noticed that I didn't need the entire PrankTargetArgumentViewModel and a simple List of Arguments would have been fine.

我更改了PrankArguments视图以采用IEnumerable并使用它;

I alterd my PrankArguments view to take an IEnumerable and used;

@using (Html.BeginForm())
{
@Html.EditorForModel()

<p>
    <input type="submit" value="Finish" />
</p>
}

然后有我的回发方法签名

then had my post back method signature like this

 [HttpPost]
 public ActionResult PrankArguments(IEnumerable<PrankArgument> arguments)

这正是我想要的.

感谢所有的建议.

这篇关于Asp.Net MVC3-复杂类未在[HttpPost]方法上作为null传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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