问题
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 class that implements an INationality
interface:
<button class="btn" type="submit" id="process-english-submission" formaction="ProcessSubmission" TODO PARAM= new EnglishNationality() formmethod="post">Process English Submission</button>
<button class="btn" type="submit" id="process-welsh-submission" formaction="ProcessSubmission" TODO PARAM= new WelshNationality() formmethod="post">Process Welsh Submission</button>
In this scenario, we'd have an INationality
interface, with EnglishNationality
and WelshNationality
classes implementating INationality
.
Can we do this in the part above where it says TODO PARAM= new EnglishNationality() / new WelshNationality()?
Note that we have a better solution than our current one, as answered in this SO post.
However, being able to pass Class instances would be an even better solution in future.
来源:https://stackoverflow.com/questions/61616599/asp-net-core-pass-class-instance-parameter-from-razor-view-to-formaction