0

its me... yet again!

Ive got these class,

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

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

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

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

My HTML is like this..

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

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

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.

so the flow goes like this.

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!

any help would be great! cheers, Ste!

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

EDIT - Removed view bag debug properties!

tereško
  • 58,060
  • 25
  • 98
  • 150
Steoates
  • 3,058
  • 5
  • 27
  • 43
  • Your question is a little bit unclear... do you mean: "why is ViewBag.Model null on every post?" – Tomas Jansson Aug 16 '11 at 07:47
  • Also, why are you setting the property on the ViewBag? – Tomas Jansson Aug 16 '11 at 07:49
  • Sorry guys - that was purely for debugging which I forgot to remove.. Hopefully the question is abit more clear. – Steoates Aug 16 '11 at 07:53
  • If you debug your action that you post back to, what does your Request.Form object contain? Have the values been posted OK but not parsed into the model? – Timbo Aug 16 '11 at 08:07
  • I've look at the Request.Forms contents and its posted back Request.Form = {argument.Value=Address&argument.Value=UnMute} which are the values im looking for - its not ideal but I could work with them! – Steoates Aug 16 '11 at 08:17

2 Answers2

0

I think if I understand you correctly if your view is strongly typed to PrankTargetArgumentViewModel then all you have to do to retrieve the values is:

[HttpPost]
public ActionResult PrankArguments()
{ 
    var pta = new PrankTargetArgumentViewModel();
    TryUpdateModel(pta);
}
Seth
  • 8,213
  • 14
  • 71
  • 103
  • @stephen is the view strongly typed to PrankTargetArgumentViewModel? i.e in your view at the top you have: `@Model PrankTargetArgumentViewModel` and in your view you could try `@Html.EditorForModel()` not `@Html.EditorFor(x =>model)` – Seth Aug 17 '11 at 02:37
0

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.

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)

which worked exactly how I wanted.

Thanks for all the suggestions guys.

Steoates
  • 3,058
  • 5
  • 27
  • 43