0

I have created an SharePoint MVC provider hosted App Part. In my app part I have a form. This form has 2 actions. One is the httpget action and the other is the httppost action. It looks like the httppost action is fired initial. What is going wrong?

This is my reference from the app part to the mvc controller:

<?xml version="1.0" encoding="utf-8"?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <ClientWebPart Name="AanmakenPeriode" Title="Periode Aanmaken" Description="Het formulier om een periode aan te kunnen maken."> <!-- Content element identifies the location of the page that will render inside the client web part Properties are referenced on the query string using the pattern _propertyName_ Example: Src="~appWebUrl/Pages/ClientWebPart1.aspx?Property1=_property1_" --> <Content Type="html" Src="~remoteAppUrl/CreatePeriode?{StandardTokens}" /> <!-- Define properties in the Properties element. Remember to put Property Name on the Src attribute of the Content element above. --> <Properties> </Properties> </ClientWebPart> </Elements> 

These are my actions in my controller:

 [HttpGet] public ActionResult Index() { return View(); } [HttpPost] [SharePointContextFilter] public ActionResult Index(PeriodeModel model) { if (ModelState.IsValid) { var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext); using (var clientContext = spContext.CreateUserClientContextForSPHost()) { if (clientContext != null) { // some logic to add a new list item, subsite etc. } } } } 

This is my form in the view:

@model MyNameSpace.Models.PeriodeModel @{ Layout = null; } @if(TempData["notice"] != null) { <h2>@TempData["notice"]</h2> } @using (Html.BeginForm()) { @Html.Label("Omschrijving") <br /> @Html.TextBoxFor(m => m.PeriodeOmschrijving) <br /> @Html.ValidationMessageFor(m => m.PeriodeOmschrijving) <br /> <br /> @Html.Label("Vanaf") <br /> @Html.TextBoxFor(m => m.PeriodeVanaf) <br /> @Html.ValidationMessageFor(m => m.PeriodeVanaf) <br /> <br /> @Html.Label("T/M") <br /> @Html.TextBoxFor(m => m.PeriodeTM) <br /> @Html.ValidationMessageFor(m => m.PeriodeTM) <br /> <br /> <input type="submit" value="Aanmaken" /> } 

1 Answer 1

1

Both the method names are same, rename the POST method as below. Looks like there is some issue with routing mechanism not able to invoke the correct method for HTTP request.

[HttpGet] public ActionResult Index() { return View(); } [HttpPost] [SharePointContextFilter] public ActionResult IndexPost(PeriodeModel model) { if (ModelState.IsValid) { var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext); using (var clientContext = spContext.CreateUserClientContextForSPHost()) { if (clientContext != null) { // some logic to add a new list item, subsite etc. } } } } 

However, if you want to keep the same name , try the older version of MVC code as below:

[AcceptVerbs("GET")] public ActionResult Index() { return View(); } [AcceptVerbs("POST")] [SharePointContextFilter] public ActionResult IndexPost(PeriodeModel model) { if (ModelState.IsValid) { var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext); using (var clientContext = spContext.CreateUserClientContextForSPHost()) { if (clientContext != null) { // some logic to add a new list item, subsite etc. } } } } 

Probable explanation -

When a POST request for is received, the action invoker creates a list of all methods of the controller that match the Index action name. In this case, we would end up with a list of two methods. Afterwards, the invoker looks at all of the ActionSelectionAttribute instances applied to each method and calls the IsValidForRequest() method on each. If each attribute returns true, then the method is considered valid for the current action.

Reference - How a Method Becomes An Action

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.