Skip to content

Instantly share code, notes, and snippets.

@Kavignon
Last active July 12, 2019 17:55
Show Gist options
  • Save Kavignon/5e1a132d6532754a7ad41b9f229ae8a9 to your computer and use it in GitHub Desktop.
Save Kavignon/5e1a132d6532754a7ad41b9f229ae8a9 to your computer and use it in GitHub Desktop.
ASP.NET Core POST Request failing silently
namespace FailingPost
{
// using directives omitted.
public class FoodGuideController
{
// Code omitted...
[HttpGet]
public IActionResult Index(PageModel model) => this.View(model ?? InitialPageModel);
[HttpGet]
public async Task<IAsyncResult> GetSubscriptionRecipes(string weeklySubscriptionModel)
{
// code omitted.
var recipes = GetWeeklyRecipes(weeklySubscriptionModel);
return recipes != null
? this.PartialView("Recipes", new PageModel { RecipeSection = recipes })
: this.BadRequest();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IAsyncResult> SubmitRecipeSelection(PageModel model)
{
if(!this.ModelState.IsValid || model.RecipeSection.Recipes == null)
{
return this.BadRequest();
}
// Do things...
}
}
}
// using directives omitted.
@model FailingPost.PageModel
<form method="post" asp-action="SubmitRecipeSelection" http-equiv="content-type" content="text/html; charset=utf-8">
<section class="SomeCssClass" id="subsriptionsection>
@*HTML and C# omitted here.*@
</section>
<section class="SomeCssClass" id="recipesection>
@{ await Html.RenderPartialAsync("Recipes", Model); }
</section>
<section class="SomeCssClass" id="selectionsection>
@*HTML and C# omitted here.*@
</section>
</form>
namespace FailingPost
{
// using directives omitted.
public class Ingredient
{
public string Name => "DummyIngredientName";
public Guid ID => Guid.NewGuid();
}
}
namespace FailingPost
{
// using directives omitted.
public class PageModel
{
public SubscriptionComponent SubscriptionSection { get; set }
public RecipeComponent RecipeSection { get; set; }
public PeriodSelectionComponent SelectionSection { get; set; }
}
}
namespace FailingPost
{
// using directives omitted.
public class Recipe
{
public Recipe(IReadonlyCollection<Ingredient> ingredients) => (Ingredient[])this.Ingredients = ingredients;
pulic string Name => "DummyRecipeName";
public Guid ID => Guid.NewGuid();
public Ingredient[] Ingredients { get; }
}
}
namespace FailingPost
{
// using directives omitted.
public class RecipeComponent
{
public IEnumerable<Recipe> Recipes { get; set; }
// code omitted.
}
}
// using directives omitted.
@model FailingPost.PageModel
<section class="SomeOtherCssClass">
@if(Model.RecipeSection != null && Model.RecipeSection.Any())
{
for(int recipeIndex = 0; recipeIndex < Model.RecipeSection.Recipes.Count(); recipeIndex++)
{
Recipe recipe = Model.RecipeSection.Recipes.ElementAt(recipeIndex);
@Html.HiddenFor(m => m.RecipeSection.Recipes.ElementAt(recipeIndex);
<div id="@recipe.RecipeID" class="SomeNewCssClass">
<h5>@recipe.Name</h5>
@for(int ingredientIndex = 0; ingredientIndex < recipe.Ingredients.Length; ingredientIndex++)
{
<div class="CssClass">
<label>@recipe.Ingredients[ingredientIndex].Name</label>
@Html.CheckboxFor(_ => @recipe.Ingredients[ingredientIndex].IsEnabled, @recipe.Ingredients[ingredientIndex].CanUserSelectIngredient ? (object) new {style = "margin-left: 10px;", @id=@recipe.Ingredients[ingredientIndex].ID } : (object) new {@disabled = "disabled", style = "margin-left: 10px;", @id=@recipe.Ingredients[ingredientIndex].ID})
</div>
}
</div>
}
}
</section>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment