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
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
add a comment |
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
1
you should create a new model (maybe call itCreateGamePostModel
) which has all the same fields as Game plus CreatorUserId. Have the Create action useCreateGamePostModel
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
add a comment |
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
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
razor entity-framework-6 asp.net-core-mvc
asked Mar 8 at 18:51
SkyeBoniwellSkyeBoniwell
2,22374589
2,22374589
1
you should create a new model (maybe call itCreateGamePostModel
) which has all the same fields as Game plus CreatorUserId. Have the Create action useCreateGamePostModel
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
add a comment |
1
you should create a new model (maybe call itCreateGamePostModel
) which has all the same fields as Game plus CreatorUserId. Have the Create action useCreateGamePostModel
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
add a comment |
1 Answer
1
active
oldest
votes
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)
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
add a comment |
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)
add a comment |
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)
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)
answered Mar 11 at 7:07
Nan YuNan Yu
7,4352763
7,4352763
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 useCreateGamePostModel
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