How to get tuple values in IActionResult net core 2
up vote
0
down vote
favorite
I have a view with two model.
This is my cshtml code:
@model Tuple<FITSWeb.Models.Test, FITSWeb.Models.Resultat>
<div class="modal-body form-horizontal">
<div class="row">
<div class="col-lg-12">
<div class="modal-header">
<h5 class="modal-title" id="ModalLabel">Selection du résultat</h5>
</div>
<div class="form-group" style="padding:10px">
<label class="control-label">Démarche</label>
<textarea readonly rows="3" class="form-control">@Model.Item1.Demarche</textarea>
<label class="control-label">Jeu d'entrée</label>
<textarea readonly rows="3" class="form-control">@Model.Item1.JeuEntree</textarea>
<label class="control-label">Résultat attendu</label>
<textarea readonly rows="3" class="form-control">@Model.Item1.ResultatAttendu</textarea>
</div>
<div class="modal-body">
Selectionner le résutat retenu pour :
</div>
<div class="form-group" style="padding:10px">
<label asp-for="@Model.Item2.Commentaire" class="control-label">Commentaire</label>
<textarea rows="3" asp-for="@Model.Item2.Commentaire" class="form-control"></textarea>
<span asp-validation-for="@Model.Item2.Commentaire" class="text-danger"></span>
</div>
<div class="modal-footer">
<input type="submit" value="Enregistrer" class="btn btn-primary mb-2" />
</div>
</div>
</div>
</div>
and the cs code:
public async Task<IActionResult> AddResult(long id)
{
Resultat TResultat = new Resultat();
var test = await _context.Test.Where(m => m.Id == id).Include(i => i.Resultats).FirstOrDefaultAsync();
if (test != null)
{
TResultat = await _context.Resultat.Where(m => m.Id == test.ResultatRef.Id).FirstOrDefaultAsync();
}
return PartialView("~/Views/Tests/_Result.cshtml", Tuple.Create<Test, Resultat>(test, TResultat));
}
return View();
}
how can i get tuple values for 'test' and 'Tresultat' after submit?
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AddResult(long id, [Bind("Numero,Condition,Demarche,JeuEntree,ResultatAttendu,Utilisateur,DateCreation,DateModification,EstActif,Id")] Test test,
[Bind("IdTest,IdSession,Commentaire,EtatActuel,Utilisateur,DateCreation,Id")] Resultat TResultat)
{...}
This code don't return tuple values, and i don't find the good solution.
c# razor .net-core tuples
add a comment |
up vote
0
down vote
favorite
I have a view with two model.
This is my cshtml code:
@model Tuple<FITSWeb.Models.Test, FITSWeb.Models.Resultat>
<div class="modal-body form-horizontal">
<div class="row">
<div class="col-lg-12">
<div class="modal-header">
<h5 class="modal-title" id="ModalLabel">Selection du résultat</h5>
</div>
<div class="form-group" style="padding:10px">
<label class="control-label">Démarche</label>
<textarea readonly rows="3" class="form-control">@Model.Item1.Demarche</textarea>
<label class="control-label">Jeu d'entrée</label>
<textarea readonly rows="3" class="form-control">@Model.Item1.JeuEntree</textarea>
<label class="control-label">Résultat attendu</label>
<textarea readonly rows="3" class="form-control">@Model.Item1.ResultatAttendu</textarea>
</div>
<div class="modal-body">
Selectionner le résutat retenu pour :
</div>
<div class="form-group" style="padding:10px">
<label asp-for="@Model.Item2.Commentaire" class="control-label">Commentaire</label>
<textarea rows="3" asp-for="@Model.Item2.Commentaire" class="form-control"></textarea>
<span asp-validation-for="@Model.Item2.Commentaire" class="text-danger"></span>
</div>
<div class="modal-footer">
<input type="submit" value="Enregistrer" class="btn btn-primary mb-2" />
</div>
</div>
</div>
</div>
and the cs code:
public async Task<IActionResult> AddResult(long id)
{
Resultat TResultat = new Resultat();
var test = await _context.Test.Where(m => m.Id == id).Include(i => i.Resultats).FirstOrDefaultAsync();
if (test != null)
{
TResultat = await _context.Resultat.Where(m => m.Id == test.ResultatRef.Id).FirstOrDefaultAsync();
}
return PartialView("~/Views/Tests/_Result.cshtml", Tuple.Create<Test, Resultat>(test, TResultat));
}
return View();
}
how can i get tuple values for 'test' and 'Tresultat' after submit?
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AddResult(long id, [Bind("Numero,Condition,Demarche,JeuEntree,ResultatAttendu,Utilisateur,DateCreation,DateModification,EstActif,Id")] Test test,
[Bind("IdTest,IdSession,Commentaire,EtatActuel,Utilisateur,DateCreation,Id")] Resultat TResultat)
{...}
This code don't return tuple values, and i don't find the good solution.
c# razor .net-core tuples
2
Would it not be easier to create a custom view model to hold this data, and just get that back on the server? I know Tuples had problems with ModelBinding as they dont have a default empty Constructor. How are you getting back your id? As it does not exist on the view?
– R2D2
Nov 7 at 9:26
As you suggested to me, I created a custom model with the necessary attributes of the two basic models. and it works very well: !! Thank you so much!
– Alematt
Nov 7 at 9:47
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a view with two model.
This is my cshtml code:
@model Tuple<FITSWeb.Models.Test, FITSWeb.Models.Resultat>
<div class="modal-body form-horizontal">
<div class="row">
<div class="col-lg-12">
<div class="modal-header">
<h5 class="modal-title" id="ModalLabel">Selection du résultat</h5>
</div>
<div class="form-group" style="padding:10px">
<label class="control-label">Démarche</label>
<textarea readonly rows="3" class="form-control">@Model.Item1.Demarche</textarea>
<label class="control-label">Jeu d'entrée</label>
<textarea readonly rows="3" class="form-control">@Model.Item1.JeuEntree</textarea>
<label class="control-label">Résultat attendu</label>
<textarea readonly rows="3" class="form-control">@Model.Item1.ResultatAttendu</textarea>
</div>
<div class="modal-body">
Selectionner le résutat retenu pour :
</div>
<div class="form-group" style="padding:10px">
<label asp-for="@Model.Item2.Commentaire" class="control-label">Commentaire</label>
<textarea rows="3" asp-for="@Model.Item2.Commentaire" class="form-control"></textarea>
<span asp-validation-for="@Model.Item2.Commentaire" class="text-danger"></span>
</div>
<div class="modal-footer">
<input type="submit" value="Enregistrer" class="btn btn-primary mb-2" />
</div>
</div>
</div>
</div>
and the cs code:
public async Task<IActionResult> AddResult(long id)
{
Resultat TResultat = new Resultat();
var test = await _context.Test.Where(m => m.Id == id).Include(i => i.Resultats).FirstOrDefaultAsync();
if (test != null)
{
TResultat = await _context.Resultat.Where(m => m.Id == test.ResultatRef.Id).FirstOrDefaultAsync();
}
return PartialView("~/Views/Tests/_Result.cshtml", Tuple.Create<Test, Resultat>(test, TResultat));
}
return View();
}
how can i get tuple values for 'test' and 'Tresultat' after submit?
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AddResult(long id, [Bind("Numero,Condition,Demarche,JeuEntree,ResultatAttendu,Utilisateur,DateCreation,DateModification,EstActif,Id")] Test test,
[Bind("IdTest,IdSession,Commentaire,EtatActuel,Utilisateur,DateCreation,Id")] Resultat TResultat)
{...}
This code don't return tuple values, and i don't find the good solution.
c# razor .net-core tuples
I have a view with two model.
This is my cshtml code:
@model Tuple<FITSWeb.Models.Test, FITSWeb.Models.Resultat>
<div class="modal-body form-horizontal">
<div class="row">
<div class="col-lg-12">
<div class="modal-header">
<h5 class="modal-title" id="ModalLabel">Selection du résultat</h5>
</div>
<div class="form-group" style="padding:10px">
<label class="control-label">Démarche</label>
<textarea readonly rows="3" class="form-control">@Model.Item1.Demarche</textarea>
<label class="control-label">Jeu d'entrée</label>
<textarea readonly rows="3" class="form-control">@Model.Item1.JeuEntree</textarea>
<label class="control-label">Résultat attendu</label>
<textarea readonly rows="3" class="form-control">@Model.Item1.ResultatAttendu</textarea>
</div>
<div class="modal-body">
Selectionner le résutat retenu pour :
</div>
<div class="form-group" style="padding:10px">
<label asp-for="@Model.Item2.Commentaire" class="control-label">Commentaire</label>
<textarea rows="3" asp-for="@Model.Item2.Commentaire" class="form-control"></textarea>
<span asp-validation-for="@Model.Item2.Commentaire" class="text-danger"></span>
</div>
<div class="modal-footer">
<input type="submit" value="Enregistrer" class="btn btn-primary mb-2" />
</div>
</div>
</div>
</div>
and the cs code:
public async Task<IActionResult> AddResult(long id)
{
Resultat TResultat = new Resultat();
var test = await _context.Test.Where(m => m.Id == id).Include(i => i.Resultats).FirstOrDefaultAsync();
if (test != null)
{
TResultat = await _context.Resultat.Where(m => m.Id == test.ResultatRef.Id).FirstOrDefaultAsync();
}
return PartialView("~/Views/Tests/_Result.cshtml", Tuple.Create<Test, Resultat>(test, TResultat));
}
return View();
}
how can i get tuple values for 'test' and 'Tresultat' after submit?
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AddResult(long id, [Bind("Numero,Condition,Demarche,JeuEntree,ResultatAttendu,Utilisateur,DateCreation,DateModification,EstActif,Id")] Test test,
[Bind("IdTest,IdSession,Commentaire,EtatActuel,Utilisateur,DateCreation,Id")] Resultat TResultat)
{...}
This code don't return tuple values, and i don't find the good solution.
c# razor .net-core tuples
c# razor .net-core tuples
asked Nov 7 at 9:23
Alematt
177
177
2
Would it not be easier to create a custom view model to hold this data, and just get that back on the server? I know Tuples had problems with ModelBinding as they dont have a default empty Constructor. How are you getting back your id? As it does not exist on the view?
– R2D2
Nov 7 at 9:26
As you suggested to me, I created a custom model with the necessary attributes of the two basic models. and it works very well: !! Thank you so much!
– Alematt
Nov 7 at 9:47
add a comment |
2
Would it not be easier to create a custom view model to hold this data, and just get that back on the server? I know Tuples had problems with ModelBinding as they dont have a default empty Constructor. How are you getting back your id? As it does not exist on the view?
– R2D2
Nov 7 at 9:26
As you suggested to me, I created a custom model with the necessary attributes of the two basic models. and it works very well: !! Thank you so much!
– Alematt
Nov 7 at 9:47
2
2
Would it not be easier to create a custom view model to hold this data, and just get that back on the server? I know Tuples had problems with ModelBinding as they dont have a default empty Constructor. How are you getting back your id? As it does not exist on the view?
– R2D2
Nov 7 at 9:26
Would it not be easier to create a custom view model to hold this data, and just get that back on the server? I know Tuples had problems with ModelBinding as they dont have a default empty Constructor. How are you getting back your id? As it does not exist on the view?
– R2D2
Nov 7 at 9:26
As you suggested to me, I created a custom model with the necessary attributes of the two basic models. and it works very well: !! Thank you so much!
– Alematt
Nov 7 at 9:47
As you suggested to me, I created a custom model with the necessary attributes of the two basic models. and it works very well: !! Thank you so much!
– Alematt
Nov 7 at 9:47
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
You can try something like this(Please note this is only a sample); your "About" csthml page:
@model Tuple<M1, M2>
<form asp-action="AddResult">
<input name="blah1" value="@Model.Item1.Field1" />
<input name="blah2" value="@Model.Item2.Field2" />
<button type="submit">Submit</button>
</form>
The backend GET action:
public IActionResult About()
{
ViewData["Message"] = "Your application description page.";
var vm = new Tuple<M1, M2>(new M1(), new M2());
return View(vm);
}
The backend POST action:
[HttpPost]
public IActionResult AddResult(MyViewModel o)
{
return RedirectToAction(nameof(About));
}
The models:
public class MyViewModel
{
public string Blah1 { get; set; }
public string Blah2 { get; set; }
}
public class M1
{
public string Field1 { get; set; }
}
public class M2
{
public string Field2 { get; set; }
}
In my opinion, you should return only one view model object instead of Tuple in the GET action because it would give you more flexibility and its easier to maintain.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You can try something like this(Please note this is only a sample); your "About" csthml page:
@model Tuple<M1, M2>
<form asp-action="AddResult">
<input name="blah1" value="@Model.Item1.Field1" />
<input name="blah2" value="@Model.Item2.Field2" />
<button type="submit">Submit</button>
</form>
The backend GET action:
public IActionResult About()
{
ViewData["Message"] = "Your application description page.";
var vm = new Tuple<M1, M2>(new M1(), new M2());
return View(vm);
}
The backend POST action:
[HttpPost]
public IActionResult AddResult(MyViewModel o)
{
return RedirectToAction(nameof(About));
}
The models:
public class MyViewModel
{
public string Blah1 { get; set; }
public string Blah2 { get; set; }
}
public class M1
{
public string Field1 { get; set; }
}
public class M2
{
public string Field2 { get; set; }
}
In my opinion, you should return only one view model object instead of Tuple in the GET action because it would give you more flexibility and its easier to maintain.
add a comment |
up vote
1
down vote
You can try something like this(Please note this is only a sample); your "About" csthml page:
@model Tuple<M1, M2>
<form asp-action="AddResult">
<input name="blah1" value="@Model.Item1.Field1" />
<input name="blah2" value="@Model.Item2.Field2" />
<button type="submit">Submit</button>
</form>
The backend GET action:
public IActionResult About()
{
ViewData["Message"] = "Your application description page.";
var vm = new Tuple<M1, M2>(new M1(), new M2());
return View(vm);
}
The backend POST action:
[HttpPost]
public IActionResult AddResult(MyViewModel o)
{
return RedirectToAction(nameof(About));
}
The models:
public class MyViewModel
{
public string Blah1 { get; set; }
public string Blah2 { get; set; }
}
public class M1
{
public string Field1 { get; set; }
}
public class M2
{
public string Field2 { get; set; }
}
In my opinion, you should return only one view model object instead of Tuple in the GET action because it would give you more flexibility and its easier to maintain.
add a comment |
up vote
1
down vote
up vote
1
down vote
You can try something like this(Please note this is only a sample); your "About" csthml page:
@model Tuple<M1, M2>
<form asp-action="AddResult">
<input name="blah1" value="@Model.Item1.Field1" />
<input name="blah2" value="@Model.Item2.Field2" />
<button type="submit">Submit</button>
</form>
The backend GET action:
public IActionResult About()
{
ViewData["Message"] = "Your application description page.";
var vm = new Tuple<M1, M2>(new M1(), new M2());
return View(vm);
}
The backend POST action:
[HttpPost]
public IActionResult AddResult(MyViewModel o)
{
return RedirectToAction(nameof(About));
}
The models:
public class MyViewModel
{
public string Blah1 { get; set; }
public string Blah2 { get; set; }
}
public class M1
{
public string Field1 { get; set; }
}
public class M2
{
public string Field2 { get; set; }
}
In my opinion, you should return only one view model object instead of Tuple in the GET action because it would give you more flexibility and its easier to maintain.
You can try something like this(Please note this is only a sample); your "About" csthml page:
@model Tuple<M1, M2>
<form asp-action="AddResult">
<input name="blah1" value="@Model.Item1.Field1" />
<input name="blah2" value="@Model.Item2.Field2" />
<button type="submit">Submit</button>
</form>
The backend GET action:
public IActionResult About()
{
ViewData["Message"] = "Your application description page.";
var vm = new Tuple<M1, M2>(new M1(), new M2());
return View(vm);
}
The backend POST action:
[HttpPost]
public IActionResult AddResult(MyViewModel o)
{
return RedirectToAction(nameof(About));
}
The models:
public class MyViewModel
{
public string Blah1 { get; set; }
public string Blah2 { get; set; }
}
public class M1
{
public string Field1 { get; set; }
}
public class M2
{
public string Field2 { get; set; }
}
In my opinion, you should return only one view model object instead of Tuple in the GET action because it would give you more flexibility and its easier to maintain.
edited Nov 7 at 11:19
answered Nov 7 at 11:00
GoldenAge
29910
29910
add a comment |
add a comment |
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%2f53186564%2fhow-to-get-tuple-values-in-iactionresult-net-core-2%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
2
Would it not be easier to create a custom view model to hold this data, and just get that back on the server? I know Tuples had problems with ModelBinding as they dont have a default empty Constructor. How are you getting back your id? As it does not exist on the view?
– R2D2
Nov 7 at 9:26
As you suggested to me, I created a custom model with the necessary attributes of the two basic models. and it works very well: !! Thank you so much!
– Alematt
Nov 7 at 9:47