Pass an extra parameter that is not part of the model into the controller The Next CEO of Stack OverflowASP MVC 3 RAZOR dynamic form generation postIs there a bug in MVC3 Razor @ifPassing Querystring value into View ModelMVC 3 post model and additional parameter to HttpPost action method using Ajax formModel binder does not fill items in nested listsPass parameter to controller from @Html.ActionLink MVC 4ASP.Net MVC form post can't bind model list propertyAsp Net MVC form causes source html to be displayed as outputSelect Box Not Populated On View After Model State Is InvalidHtml.TextBoxFor input inside for each loop not working

Do I need to write [sic] when including a quotation with a number less than 10 that isn't written out?

pgfplots: How to draw a tangent graph below two others?

Shortening a title without changing its meaning

How can I replace x-axis labels with pre-determined symbols?

How to unfasten electrical subpanel attached with ramset

How dangerous is XSS

Calculate the Mean mean of two numbers

Is a distribution that is normal, but highly skewed, considered Gaussian?

Can this transistor (2n2222) take 6V on emitter-base? Am I reading datasheet incorrectly?

Which acid/base does a strong base/acid react when added to a buffer solution?

MT "will strike" & LXX "will watch carefully" (Gen 3:15)?

How should I connect my cat5 cable to connectors having an orange-green line?

Raspberry pi 3 B with Ubuntu 18.04 server arm64: what pi version

Why can't we say "I have been having a dog"?

Man transported from Alternate World into ours by a Neutrino Detector

How can I separate the number from the unit in argument?

Creating a script with console commands

Is there a rule of thumb for determining the amount one should accept for a settlement offer?

How do I secure a TV wall mount?

Can Sri Krishna be called 'a person'?

Could a dragon use its wings to swim?

Incomplete cube

Does int main() need a declaration on C++?

Finitely generated matrix groups whose eigenvalues are all algebraic



Pass an extra parameter that is not part of the model into the controller



The Next CEO of Stack OverflowASP MVC 3 RAZOR dynamic form generation postIs there a bug in MVC3 Razor @ifPassing Querystring value into View ModelMVC 3 post model and additional parameter to HttpPost action method using Ajax formModel binder does not fill items in nested listsPass parameter to controller from @Html.ActionLink MVC 4ASP.Net MVC form post can't bind model list propertyAsp Net MVC form causes source html to be displayed as outputSelect Box Not Populated On View After Model State Is InvalidHtml.TextBoxFor input inside for each loop not working










0















I have the code below that shows my Create cshtml page and the controller connected to it.



It's working fine, however now I need to add another field to the form that is not in the GameManagement.Game model.



This field is called "CreatorUserId" and does not exist in the model I'm using.



But I do I get the value of this field, and then pass it into the controller when it's not part of the model?



Thanks!



Create.cshtml:



@model GameManagement.Game


<div>
<div>
<form asp-action="Create">
<div>
<label asp-for="Id"></label>
<input asp-for="Id" />
</div>
<div>
<label asp-for="Description"></label>
<input asp-for="Description" />
</div>
<div>
<label asp-for="DisplayName"></label>
<input asp-for="DisplayName" />
</div>
<div>
<label>Creator User ID</label>
<input id="CreatorUserId" />
<div>
<input type="submit" value="Create" />
</div>
</form>
</div>
</div>


Controller:



[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Description,DisplayName")] Game newGame)


// code to send the form information (Id, Description, DisplayName) to a 3rd party API

apiResult = await apiClient.createNewGame(
newGame.Id,
newGame.Description,
newGame.DisplayName,
// CreatorUserId ?? not in Game model....

return View(apiResult);










share|improve this question

















  • 1





    you should create a new model (maybe call it CreateGamePostModel) which has all the same fields as Game plus CreatorUserId. Have the Create action use CreateGamePostModel as its only parameter). Then, when you post, you can transfer all the required fields to Game and then do whatever you want with the CreatedUserId field

    – johnluke.laue
    Mar 8 at 22:03
















0















I have the code below that shows my Create cshtml page and the controller connected to it.



It's working fine, however now I need to add another field to the form that is not in the GameManagement.Game model.



This field is called "CreatorUserId" and does not exist in the model I'm using.



But I do I get the value of this field, and then pass it into the controller when it's not part of the model?



Thanks!



Create.cshtml:



@model GameManagement.Game


<div>
<div>
<form asp-action="Create">
<div>
<label asp-for="Id"></label>
<input asp-for="Id" />
</div>
<div>
<label asp-for="Description"></label>
<input asp-for="Description" />
</div>
<div>
<label asp-for="DisplayName"></label>
<input asp-for="DisplayName" />
</div>
<div>
<label>Creator User ID</label>
<input id="CreatorUserId" />
<div>
<input type="submit" value="Create" />
</div>
</form>
</div>
</div>


Controller:



[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Description,DisplayName")] Game newGame)


// code to send the form information (Id, Description, DisplayName) to a 3rd party API

apiResult = await apiClient.createNewGame(
newGame.Id,
newGame.Description,
newGame.DisplayName,
// CreatorUserId ?? not in Game model....

return View(apiResult);










share|improve this question

















  • 1





    you should create a new model (maybe call it CreateGamePostModel) which has all the same fields as Game plus CreatorUserId. Have the Create action use CreateGamePostModel as its only parameter). Then, when you post, you can transfer all the required fields to Game and then do whatever you want with the CreatedUserId field

    – johnluke.laue
    Mar 8 at 22:03














0












0








0








I have the code below that shows my Create cshtml page and the controller connected to it.



It's working fine, however now I need to add another field to the form that is not in the GameManagement.Game model.



This field is called "CreatorUserId" and does not exist in the model I'm using.



But I do I get the value of this field, and then pass it into the controller when it's not part of the model?



Thanks!



Create.cshtml:



@model GameManagement.Game


<div>
<div>
<form asp-action="Create">
<div>
<label asp-for="Id"></label>
<input asp-for="Id" />
</div>
<div>
<label asp-for="Description"></label>
<input asp-for="Description" />
</div>
<div>
<label asp-for="DisplayName"></label>
<input asp-for="DisplayName" />
</div>
<div>
<label>Creator User ID</label>
<input id="CreatorUserId" />
<div>
<input type="submit" value="Create" />
</div>
</form>
</div>
</div>


Controller:



[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Description,DisplayName")] Game newGame)


// code to send the form information (Id, Description, DisplayName) to a 3rd party API

apiResult = await apiClient.createNewGame(
newGame.Id,
newGame.Description,
newGame.DisplayName,
// CreatorUserId ?? not in Game model....

return View(apiResult);










share|improve this question














I have the code below that shows my Create cshtml page and the controller connected to it.



It's working fine, however now I need to add another field to the form that is not in the GameManagement.Game model.



This field is called "CreatorUserId" and does not exist in the model I'm using.



But I do I get the value of this field, and then pass it into the controller when it's not part of the model?



Thanks!



Create.cshtml:



@model GameManagement.Game


<div>
<div>
<form asp-action="Create">
<div>
<label asp-for="Id"></label>
<input asp-for="Id" />
</div>
<div>
<label asp-for="Description"></label>
<input asp-for="Description" />
</div>
<div>
<label asp-for="DisplayName"></label>
<input asp-for="DisplayName" />
</div>
<div>
<label>Creator User ID</label>
<input id="CreatorUserId" />
<div>
<input type="submit" value="Create" />
</div>
</form>
</div>
</div>


Controller:



[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Description,DisplayName")] Game newGame)


// code to send the form information (Id, Description, DisplayName) to a 3rd party API

apiResult = await apiClient.createNewGame(
newGame.Id,
newGame.Description,
newGame.DisplayName,
// CreatorUserId ?? not in Game model....

return View(apiResult);







razor entity-framework-6 asp.net-core-mvc






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 18:51









SkyeBoniwellSkyeBoniwell

2,22374589




2,22374589







  • 1





    you should create a new model (maybe call it CreateGamePostModel) which has all the same fields as Game plus CreatorUserId. Have the Create action use CreateGamePostModel as its only parameter). Then, when you post, you can transfer all the required fields to Game and then do whatever you want with the CreatedUserId field

    – johnluke.laue
    Mar 8 at 22:03













  • 1





    you should create a new model (maybe call it CreateGamePostModel) which has all the same fields as Game plus CreatorUserId. Have the Create action use CreateGamePostModel as its only parameter). Then, when you post, you can transfer all the required fields to Game and then do whatever you want with the CreatedUserId field

    – johnluke.laue
    Mar 8 at 22:03








1




1





you should create a new model (maybe call it CreateGamePostModel) which has all the same fields as Game plus CreatorUserId. Have the Create action use CreateGamePostModel as its only parameter). Then, when you post, you can transfer all the required fields to Game and then do whatever you want with the CreatedUserId field

– johnluke.laue
Mar 8 at 22:03






you should create a new model (maybe call it CreateGamePostModel) which has all the same fields as Game plus CreatorUserId. Have the Create action use CreateGamePostModel as its only parameter). Then, when you post, you can transfer all the required fields to Game and then do whatever you want with the CreatedUserId field

– johnluke.laue
Mar 8 at 22:03













1 Answer
1






active

oldest

votes


















1














As @johnluke.laue suggested , the best solution is to create a new view model to include the needed properties .



If you insist on not creating a new viewmodel , the workaround could be add name attribute :



<input id="CreatorUserId" name="CreatorUserId" />


And get value on server side like :



public async Task<IActionResult> Create([Bind("Id,Description,DisplayName")] Game newGame, string CreatorUserId)







share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55069299%2fpass-an-extra-parameter-that-is-not-part-of-the-model-into-the-controller%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    As @johnluke.laue suggested , the best solution is to create a new view model to include the needed properties .



    If you insist on not creating a new viewmodel , the workaround could be add name attribute :



    <input id="CreatorUserId" name="CreatorUserId" />


    And get value on server side like :



    public async Task<IActionResult> Create([Bind("Id,Description,DisplayName")] Game newGame, string CreatorUserId)







    share|improve this answer



























      1














      As @johnluke.laue suggested , the best solution is to create a new view model to include the needed properties .



      If you insist on not creating a new viewmodel , the workaround could be add name attribute :



      <input id="CreatorUserId" name="CreatorUserId" />


      And get value on server side like :



      public async Task<IActionResult> Create([Bind("Id,Description,DisplayName")] Game newGame, string CreatorUserId)







      share|improve this answer

























        1












        1








        1







        As @johnluke.laue suggested , the best solution is to create a new view model to include the needed properties .



        If you insist on not creating a new viewmodel , the workaround could be add name attribute :



        <input id="CreatorUserId" name="CreatorUserId" />


        And get value on server side like :



        public async Task<IActionResult> Create([Bind("Id,Description,DisplayName")] Game newGame, string CreatorUserId)







        share|improve this answer













        As @johnluke.laue suggested , the best solution is to create a new view model to include the needed properties .



        If you insist on not creating a new viewmodel , the workaround could be add name attribute :



        <input id="CreatorUserId" name="CreatorUserId" />


        And get value on server side like :



        public async Task<IActionResult> Create([Bind("Id,Description,DisplayName")] Game newGame, string CreatorUserId)








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 11 at 7:07









        Nan YuNan Yu

        7,4352763




        7,4352763





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55069299%2fpass-an-extra-parameter-that-is-not-part-of-the-model-into-the-controller%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

            2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme