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.










share|improve this question


















  • 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















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.










share|improve this question


















  • 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













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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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












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.






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',
    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%2f53186564%2fhow-to-get-tuple-values-in-iactionresult-net-core-2%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








    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.






    share|improve this answer



























      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.






      share|improve this answer

























        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.






        share|improve this answer














        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 7 at 11:19

























        answered Nov 7 at 11:00









        GoldenAge

        29910




        29910






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini