Try our conversational search powered by Generative AI!

[HttpGet] method return null

Vote:
 

I have a project that is based on the EpiServer MVC. One of PageContoller have a 2 methods:

[HttpGet]
        public ActionResult Create()
        {
            var model = new Models.Comment {Time = DateTime.Now};
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(Models.Comment comment)
        {
            if (comment != null)
            {
                CommentsContainer.Add(comment);
            }
            else
            {

                var x = new Models.Comment
                {
                    User = "No user",
                    Body = "No text",
                    Time = DateTime.Now,
                };
                CommentsContainer.Add(x);
            }
            return RedirectToAction("Index");
        }

[Httpget] method have this View:

@using EPiServer.Globalization
@model EPiServer.Templates.Alloy.Models.Comment

@{
    Layout = null;
}

@using (Html.BeginForm(null, null,new { language = ContentLanguage.PreferredCulture.Name }, FormMethod.Post))
{
    @Html.LabelFor(model => model.User, "User")
    @Html.EditorFor(model => model.User)
    

@Html.LabelFor(model => model.Body, "Text") @Html.EditorFor(model => model.Body)

}


But [HttpPost] argument is always null. Anybody knows, how the solve this problem?

#119810
Apr 03, 2015 9:07
Vote:
 

Hi, Aliaksei,

You should use the following code instead for BeginForm:

@using (Html.BeginForm("Create", "StartPage", new { language = ContentLanguage.PreferredCulture.Name }, FormMethod.Post))
{
}

Instead of StartPage, use the name of your controller. Let me know if it worked.

BR,

Marija

#119821
Apr 03, 2015 11:46
Vote:
 

You can also use @Html.BeginForm(), that will render post action form to excatly the same page.

If action is hit (breakpoint) then probably model binder was not able to bind incoming parameters to model type. You can try to look into ModelState object - maybe there's something interesting to find.

#119866
Apr 04, 2015 21:12
Vote:
 

To Marija Jemuovic 

No it's not work. HttpPost method's arg is null again.

To Valdis Iljuconoks 

You can also use @Html.BeginForm() - no changes. Arg is null. 

Breakpoint is triggered in this method. He called, but his argument - is null.

[HttpPost]

public ActionResult Create(Models.Comment comment)
{}

About ModelState. How and where do you suggest to use that in this problem?

#119875
Apr 06, 2015 10:07
Vote:
 

If breakpoint is hit, that means that form is submitted to correct controller and action. Do you really use the same "Comment" model in view and in action? If so, then seems like problem us in model binder. Do you have custom model binder? ModelState is accessible via base controller class. You can review it in Watch: type "this.ModelState".

#119876
Apr 06, 2015 11:19
Vote:
 

 Do you really use the same "Comment" model in view and in action? 

Yeah, it's the same model. 

If so, then seems like problem us in model binder. Do you have custom model binder?

No, all is default.

ModelState is accessible via base controller class. You can review it in Watch: type "this.ModelState".

I done so. Z is null too

 [HttpPost]
        public ActionResult Create(Models.Comment comment)
        {
            var z = this.ModelState;
            if (comment != null)
            {
                CommentsContainer.Add(comment);
            }
        }

 

#119877
Edited, Apr 06, 2015 11:44
Vote:
 

Just re-created your setup in sample AlloyTech site - everything works as expected - model is binded and submitted values are set for the incoming model.

I still speculate that there is somebody in between incoming request and action invoke - where model binding happens :)

#119886
Apr 06, 2015 19:45
Vote:
 

It's little bit crazy, but... I created simple AlloyMVC site. Create my model Comment:

using System;
using EPiServer.Core;
using EPiServer.DataAnnotations;

namespace EPiServer.Templates.Alloy.Models.Commentaries
{
    [ContentType]
    public class Comment : IContent
    {

        public virtual string User { get; set; }
        public virtual string Body { get; set; }
        public virtual DateTime Time { get; set; }

        #region IContent
        private readonly PropertyDataCollection _properties = new PropertyDataCollection();

        public PropertyDataCollection Property
        {
            get { return _properties; }
        }

        public string Name { get; set; }

        public ContentReference ContentLink { get; set; }

        public ContentReference ParentLink { get; set; }

        public Guid ContentGuid { get; set; }

        public int ContentTypeID { get; set; }

        public bool IsDeleted { get; set; }

        public bool IsNull
        {
            get { return _properties.Count == 0; }
        }
        #endregion

    }
}

Created 2 methods in StartPageController:

[HttpGet]
        public ActionResult Create()
        {
            var model = new Comment { Time = DateTime.Now };
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(Comment comment)
        {
            if (comment != null)
            {
                CommentsContainer.Add(comment);
            }
            else
            {

                var x = new Comment
                {
                    User = "User",
                    Body = "Text",
                    Time = DateTime.Now,
                };
                CommentsContainer.Add(x);

            }
            return RedirectToAction("Index");
        }

Create View for create HttpGet method :


@using EPiServer.Globalization
@model EPiServer.Templates.Alloy.Models.Commentaries.Comment
@{
    Layout = null;
}
@using (Html.BeginForm("Create", "StartPage", new { language = ContentLanguage.PreferredCulture.Name }, FormMethod.Post))
{
    @Html.LabelFor(model => model.User, "User")
    @Html.EditorFor(model => model.User)
    <br /><br />
    @Html.LabelFor(model => model.Body, "Text")
    @Html.EditorFor(model => model.Body)
    <br /><br />
    <input type="submit" value="Add" />
}

And created a link that calls this method.

But HttpPost Method again have null.

MB it happens so when we create model it has not all fields? 

#119894
Apr 07, 2015 9:35
Vote:
 

Ok, now I understand. So the problem in your case is that ViewModel implements IContent interface.

By doing this you are tricking EPiServer to go and find a content data for this model from incoming request. Workaround for this case is not to inherit from EPiServer.Core.IContent interface. If you need those properties - you will need to declare similar interface yourself.

#119909
Apr 07, 2015 11:54
Vote:
 

Thx, it helps.

#119910
Apr 07, 2015 12:17
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.