问题
Our team has inherited a system that we're trying to improve as we go along.
One thing we've noticed in our razor Views is many cases where different Controller Actions are written, that look like they could be parameterised.
for example, we have something a bit like this...
ProcessSubmissionView.cshtml
<button class="btn" type="submit" id="process-english-submission" formaction="ProcessWelshSubmission" formmethod="post">Process Welsh Submission</button>
<button class="btn" type="submit" id="process-welsh-submission" formaction="ProcessEnglishSubmission" formmethod="post">Process English Submission</button>
...that we'd prefer to write like this, i.e. call a single action (ProcessSubmission
), but with a parameter - in this case, a 'Nationality' enum:
<button class="btn" type="submit" id="process-english-submission" formaction="ProcessSubmission" TODO PARAM= Nationality.English formmethod="post">Process English Submission</button>
<button class="btn" type="submit" id="process-welsh-submission" formaction="ProcessSubmission" TODO PARAM= Nationality.Welsh formmethod="post">Process Welsh Submission</button>
Given we have a Nationality
enum, what should we put in the part above where it says TODO PARAM= Nationality.English/Welsh?
回答1:
You can use the name
and value
attrbutes on <button type=“submit”>
.
<button type=“submit” name=“@Html.NameFor( m => m.Nationality )” value=“@( nameof(MyEnum.English) )”>english</button>
来源:https://stackoverflow.com/questions/61613066/asp-net-core-pass-enum-parameter-from-razor-view-to-formaction