I have a dotnet 3.1 Razor page with three buttons with asp-page-handler parameters inside the same form. The first and last one work (i.e., they go to the webpage with a handler query parameter equal to the string in the asp-page-handler parameter). The third one does not get the handler query parameter added.
Here's a code snippet (cshtml page):
@page
@model RMP.Pages.Client.SurveySetNameModel
@{
ViewData["Title"] = "SurveySetName";
Layout = "~/Pages/Shared/_Layout.cshtml";
}
<div class="breadcrumb">
<a class="breadcrumb-item" href="/Client/SurveyList/@Model.intUserId.ToString()">Surveys</a>
<span class="breadcrumb-item" id="breadcrumbText">Create New Survey</span>
</div>
<h1>Name your survey</h1>
<p>The name is for your purposes only, it will not be visible to people taking the survey.</p>
<form method="post">
<div>
<input asp-for="sinSurveyName" requried="required" placeholder="Survey Name" class="form-control frmInpt" />
<span asp-validation-for="sinSurveyName"></span>
</div>
<br />
<h2>Select a Type</h2>
<div class="card-deck">
<div class="card">
<div class="card-body">
<h5 class="card-title">Standard</h5>
<p>
Use our survey to find out some information.
</p>
<p>
Cost: 1 Standard credit
</p>
</div>
<div class="card-footer text-center">
@if (Model.dctTypeAvailability[1])
{
<input type="submit" class="btn btn-primary" value="Standard" asp-page-handler="Stan" />
}
else
{
<p>
<b>Not enough credits for this survey. Contact your sales rep.</b>
</p>
}
</div>
</div>
<div class="card">
<div class="card-body">
<h5 class="card-title">Standard + Custom</h5>
<p>
In addition to the standard do this
</p>
<p>Cost: 1 Standard credit and 1 Custom credit</p>
</div>
<div class="card-footer text-center" ">
@if (Model.dctTypeAvailability[3])
{
<input type="submit" class="btn btn-primary" value="Standard + Custom" asp-page-handler="StanCust" />
}
else
{
<p>
<b>Not enough credits for this survey. Contact your sales rep.</b>
</p>
}
</div>
</div>
<div class="card">
<div class="card-body">
<h5 class="card-title">Custom</h5>
<p>
Write up to 10 questions
</p>
<p>Cost: 1 custom credit</p>
</div>
<div class="card-footer text-center" ">
@if (Model.dctTypeAvailability[2])
{
<input type="submit" class="btn btn-primary" value="Custom" asp-page-handler="Cust" />
}
else
{
<p>
<b>Not enough credits for this survey. Contact your sales rep.</b>
</p>
}
</div>
</div>
</div>
</form>
<div>@Model.strFeedBack</div>
and the cs file (the relevant parts):
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Identity;
using RMP.Areas.Identity.Data;
using RMP.Classes;
using RMP.RMPModel;
namespace RMP.Pages.Client
{
public class SurveySetNameModel : PageModel
{
[BindProperty]
[Required(ErrorMessage = "Survey Name is required")]
public string sinSurveyName { get; set; }
public string sinSurveyName_validation { get; set; }
public string strFeedBack { get; set; }
public int intUserId { get; set; }
public Dictionary<int, bool> dctTypeAvailability;
UserManager<RMPUser> _userManager;
SignInManager<RMPUser> _signInManager;
dbRMPContext _dbContext;
public SurveySetNameModel(dbRMPContext dbContext, UserManager<RMPUser> userManager, SignInManager<RMPUser> SignInManager)
{
_dbContext = dbContext;
_userManager = userManager;
_signInManager = SignInManager;
}
public void OnGet()
{
intUserId = 0;
if (_signInManager.IsSignedIn(User))
{
string strUserId = _userManager.GetUserId(User);
intUserId = Int32.Parse(strUserId);
}
dctTypeAvailability = setAvailability();
// strFeedBackTest = intUserId.ToString();
}
public IActionResult OnPost(String sinSurveyName)
{
String redirectUrl = "";
if ((sinSurveyName == null) || (sinSurveyName.Trim() == ""))
{
sinSurveyName_validation = "You must fill out this field.";
}
else
{
int intCurrentUserId = 0;
// string strUserId = _userManager.GetUserId(User);
intCurrentUserId = Int32.Parse(_userManager.GetUserId(User));
int intTicTocDefault = TicTocLibrary.getApplicationDefaultTicTocPoints(_dbContext);
sinSurveyName_validation = sinSurveyName;
//Surveys.createNewSurveySubtractCredits(_dbContext, sinSurveyName, intCurrentUserId, null, )
TblSurveyInfo objSurveyInfo = new TblSurveyInfo();
objSurveyInfo.SinSurveyName = sinSurveyName;
objSurveyInfo.SinUpdatedByUserId = intCurrentUserId;
objSurveyInfo.SinCreatedByUserId = intCurrentUserId;
objSurveyInfo.SinSurveyTicTocPoints = intTicTocDefault;
objSurveyInfo.SinOnMarket = false;
objSurveyInfo.SinProductPrice = "";
objSurveyInfo.SinProductDescription = "";
objSurveyInfo.SinProductBrand = "";
objSurveyInfo.SinProductValueProposition = "";
objSurveyInfo.SinStatusId = 1;
objSurveyInfo.SinSurveyTemplateId = 1;
objSurveyInfo.SinCusId = Users.GetCustomerIdByUserId(_dbContext, intCurrentUserId);
objSurveyInfo.SinUpdateDateUtc = DateTime.UtcNow;
_dbContext.Add(objSurveyInfo);
_dbContext.SaveChanges();
// subtract a credit from the user
Credits.subtractCustomerCreditAll(_dbContext, intCurrentUserId, objSurveyInfo.SinId, objSurveyInfo.SinCusId, SurveyTypeUtils.getSurveyType(objSurveyInfo.SinStyId));
redirectUrl = "/Client/ChooseAudience/" + objSurveyInfo.SinId;
return Redirect(redirectUrl);
}
return null;
}
public IActionResult OnPostStan()
{
return GenericPost(SurveyTypeUtils.enmSurveyTypes.Standard);
}
public IActionResult OnPostStanCust()
{
return GenericPost(SurveyTypeUtils.enmSurveyTypes.StandardAndCustom);
}
public IActionResult OnPostCust()
{
return GenericPost(SurveyTypeUtils.enmSurveyTypes.Custom);
}
private IActionResult GenericPost(SurveyTypeUtils.enmSurveyTypes enmSurveyType)
{
String redirectUrl = "";
if ((sinSurveyName == null) || (sinSurveyName.Trim() == ""))
{
sinSurveyName_validation = "You must fill out this field.";
dctTypeAvailability = setAvailability();
}
else
{
int intCurrentUserId = 0;
string strMessage = "";
int intSurveyId = 0;
// string strUserId = _userManager.GetUserId(User);
intCurrentUserId = Int32.Parse(_userManager.GetUserId(User));
int intTicTocDefault = TicTocLibrary.getApplicationDefaultTicTocPoints(_dbContext);
sinSurveyName_validation = sinSurveyName;
intSurveyId = Surveys.createNewSurveySubtractCredits(_dbContext, sinSurveyName, intCurrentUserId, enmSurveyType, out strMessage);
redirectUrl = "/Client/ChooseAudience/" + intSurveyId;
return Redirect(redirectUrl);
}
return null;
}
private int doSurveyCreation(string strName, SurveyTypeUtils.enmSurveyTypes enmSurveyType)
{
int intSurveyId = 0;
int intCurrentUserId = Int32.Parse(_userManager.GetUserId(User));
string strMessage = "";
intSurveyId = Surveys.createNewSurveySubtractCredits(_dbContext, strName, intCurrentUserId, enmSurveyType, out strMessage);
return intSurveyId;
}
private Dictionary<int, bool> setAvailability()
{
Dictionary<int, bool> dctAvail = new Dictionary<int, bool>();
var typeVals = Enum.GetValues(typeof(SurveyTypeUtils.enmSurveyTypes));
SurveyTypeUtils.ISurveyType objSurveyType;
int intCustomerId = Users.GetCustomerIdByUserId(_dbContext, Int32.Parse(_userManager.GetUserId(User)));
foreach (var enmType in typeVals)
{
objSurveyType = SurveyTypeUtils.getSurveyType((SurveyTypeUtils.enmSurveyTypes)enmType);
dctAvail.Add((int)enmType, Credits.creditsAvailableForType(_dbContext, intCustomerId, objSurveyType));
}
return dctAvail;
}
}
}
So, when clicking on the button that says "Standard", I get a post to the page with ?handler="Stan" appended and when clicking on the button that says "Custom", I get a post to the page with ?handler-"Cust" appended. However, when I click on the button that says "Standard + Custom", i get a post to the page with no query parameter appended.
What gives?
What am I doing wrong?
Thanks, Owen