0

I am trying to develop a MVC controller rendering to achieve the following goal:

  1. the view has a textbox and a button, some dropdowns such as topics, and sorted by
  2. when user click on the button, I need to postback the textbox value to my controller and controller code will get some data and populate the model and return the same view
  3. the data is shown and user can use the dropdown to filter the result by topics, and also specified the sort order - this sort order and filter also need to be posted back so the controller will be able to get specific data and send back to the view again.

I tried using @Html.BeginForm and it does not work, I also tried to use something like suggested in Posting forms in Sitecore MVC (Part 2) – Controller Renderings

@using (Html.BeginRouteForm(Sitecore.Mvc.Configuration.MvcSettings.SitecoreRouteName, FormMethod.Post))
{       
    @Html.Sitecore().FormHandler("Car", "EditCar") 
    @Html.LabelFor(x => x.CarName)    
    @Html.EditorFor(x => x.CarName)
  
    @Html.LabelFor(x => x.CarManufactureYear)
    @Html.EditorFor(x => x.CarManufactureYear)
  
    <input type="submit" />
}
1
  • What is not working? What errors are you seeing? What is the result vs the expected result?
    – Richard Seal
    Commented Nov 13, 2023 at 13:35

2 Answers 2

0

Your controller will look something like:

public class SampleController : Controller
    {
        [HttpGet]
        public ActionResult SampleAction()
        {
            return this.View(new FormModel());
        }

        [HttpPost]
        public ActionResult SampleAction(FormModel model)
        {
            return this.View(model);
        }
    }

The FormModel class will be:

public class FormModel
    {
        public string CarName { get; set; }
        public string CarManufactureYear { get; set; }
    }

Please have a look here for more complex cases: https://github.com/unic/SUGCON2015/tree/master

0

When you say it does not work, what does not work? did you check console? are there any errors? are there any validations getting fired?

I can not see your controller but it should have same EditCar method available where it can post too

And your model should have properties with the same name, Can you make sure all these? and try to put debug and see what is happening

Form works the same way, so it has post method, it knows where to post, so i feel either some validations are there or it is posting but you are returning a view from your controller and you feel its not posting, putting debugger will help.

Let me know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.