0

Using a few answers here, I ended up using a Tuple<> with two Models for a single View. For simplicity, tuple.Item1 has a number known in the view, and I'm submitting info for tuple.Item2 to its controller. I want to send back the value of tuple.Item1.num in the submitted tuple.Item2, for the specific member tuple.Item2.num.

Currently, I have this for the message submission:

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "createMessage", @action = "/api/Message" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

    @Html.Hidden("ID", @Model.Item2.ID)

    <div class="editor-field">
        @Html.TextArea("Text", @Model.Item2.Text)
        @Html.ValidationMessageFor(tuple => tuple.Item2.Text)<br />
    </div>

    <input type="submit" value="Post Discussion" />
}

So, I'd like to have something sending the value of tuple.Item1.num within the Item2 (Message) Model posted to the Controller. How would I do this?

Mind you, I'm verrry new to the MVC and ASP.net frameworks, so I likely have some things mixed up. I understand that this HtmlHelper knows it's working with MessageController due to its @action attribute, but I'm still confused on how it's posting values to it. Any help would be great, thanks!

As per requested, my models:

DrugEntry.cs

namespace Project.Models
{
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using FileHelpers;

    [DelimitedRecord(",")]
    [Table("DRUGS")]
    public class DrugEntry
    {
        private string ndc;
        private string dosage;
        private string brand;
        private string generic;
        private string currentStatus;

        public DrugEntry()
        {
            this.ndc = string.Empty;
            this.dosage = string.Empty;
            this.brand = string.Empty;
            this.generic = string.Empty;
            this.currentStatus = "good"; // By default, a drug has no shortages associated with it, and thus is considered 'good'
        }

        public DrugEntry(string ndc, string dosage, string brand, string generic, string currentStatus)
        {
            this.ndc = ndc;
            this.dosage = dosage;
            this.brand = brand;
            this.generic = generic;
            this.currentStatus = currentStatus;
        }

        [Key]
        [Column("NDC")]
        public string NDC
        {
            get
            {
                return this.ndc;
            }

            set
            {
                this.ndc = value;
            }
        }

        [Column("DOSAGE")]
        public string Dosage
        {
            get
            {
                return this.dosage;
            }

            set
            {
                this.dosage = value;
            }
        }

        [Column("BRAND_NAME")]
        public string Brand
        {
            get
            {
                return this.brand;
            }

            set
            {
                this.brand = value;
            }
        }

        [Column("GENERIC_NAME")]
        public string Generic
        {
            get
            {
                return this.generic;
            }

            set
            {
                this.generic = value;
            }
        }

        [Column("CURRENT_STATUS")]
        public string CurrentStatus
        {
            get
            {
                return this.currentStatus;
            }

            set
            {
                this.currentStatus = value;
            }
        }
    }
}

Message.cs

namespace Project.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;
    using System.Web;
    using FileHelpers;

    [Table("MESSAGE")]
    public class Message
    {
        private int id;
        private int shortageID;
        private string ndc;
        private string user;
        private DateTime date;
        private string text;

        [Key]
        [Column("ID")]
        public int ID
        {
            get
            {
                return this.id;
            }

            set
            {
                this.id = value;
            }
        }

        [Column("SHORTAGE_ID")]
        public int ShortageID
        {
            get
            {
                return this.shortageID;
            }

            set
            {
                this.shortageID = value;
            }
        }

        [Column("NDC")]
        public string NDC
        {
            get
            {
                return this.ndc;
            }

            set
            {
                this.ndc = value;
            }
        }

        [Column("USER")]
        public string User
        {
            get
            {
                return this.user;
            }

            set
            {
                this.user = value;
            }
        }

        [Column("DATE")]
        public DateTime Date
        {
            get
            {
                return this.date;
            }

            set
            {
                this.date = value;
            }
        }

        [Column("TEXT")]
        public string Text
        {
            get
            {
                return this.text;
            }

            set
            {
                this.text = value;
            }
        }
    }
}
Befall
  • 6,560
  • 9
  • 24
  • 29
  • I might be able to provide a more MVC-like solution, but first I need to see the code of both models, can you provide? – Yorro Mar 15 '14 at 06:22
  • 1
    @Yorro: I added the models, let me know what a possibility is. I'm sending back input text of Message.Text to its controller, and wish to also send DrugEntry.NDC along with it. – Befall Mar 17 '14 at 14:43

2 Answers2

1

Use ViewModels

Instead of using a tuple, I would recommend to use a ViewModel. A ViewModel is just a simple class that you specifically create to meet the requirements of your view.

A ViewModel is an Asp Mvc standard, you don't modify a model just for the view, instead you create ViewModels.

What is ViewModel in MVC?

You would setup your view model like so.

// You typically name your ViewModel to the View
// that it represents.
public class MessageSubmission 
{
     public Message Message { get; set; }
     public DrugEntry DrugEntry { get; set; }
}

ViewModels should be stored in their own folder. Create a folder called ViewModels and store them there. The following is a folder structure of an applicaton created by Microsoft, notice the ViewModels folder?

enter image description here

View

Since you are using weakly-typed html extensions, I would suggest the following.

@model MyMvcApplication1.ViewModels.MessageSubmission

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "createMessage", @action = "/api/Message" }))
{
    @Html.ValidationSummary(true)

    @Html.Hidden("ID", @Model.Message.ID)

    <!-- // You can assign the DrugEntry.NDC, to a Message.NDC like this -->
    @Html.Hidden("NDC", @Model.DrugEntry.NDC) 

    <div class="editor-field">
        @Html.TextArea("Text", @Model.Message.Text)
        @Html.ValidationMessageFor(model => model.Message.Text)<br />
    </div>

    <input type="submit" value="Post Discussion" />
}

Controller

Simply setup your controller like you normally would.

[HttpPost]
public ActionResult Message(Message message)
{
        // Add your stuff here.
}

The MVC default model binder automatically assigns the values from the view page(ID,NDC,Text) to the waiting model in the controller (Message.ID, Message.NDC, Message.Text)

The binder aligns the fields by comparing the ID of the html controls to the properties of the model.

View          | Model Properties
------------------------------
Hidden.ID     | Message.ID
Hidden.NDC    | Message.NDC
TextArea.Text | Message.Text
Community
  • 1
  • 1
Yorro
  • 11,445
  • 4
  • 37
  • 47
0

You can try this in ajax

@using (Ajax.BeginForm("ActionOfItem2", "ControllerOfItem2", new AjaxOptions { UpdateTargetId = "UpdateDiv"}))
{
 @Html.Hidden("ID", @Model.Item2.ID)
 <input type="submit" value="Post Discussion" />
}

Controller

public ReturnType ActionOfItem2(string ID)
 {
   // Use the ID here
 }
Golda
  • 3,823
  • 10
  • 34
  • 67