ASP.NET Core - pass Class instance parameter from razor View to formaction

柔情痞子 提交于 2020-05-17 06:01:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!