Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm working on MVC 5 I just need to know is there any possible way that variables can be passed from controller to view? If so How should it be done? I wanna copy the values from one view(textbox) to another view(label) and show it from textbox to label please help.

THIS IS MY CONTROLLER
CSS
public ActionResult Contact(Models.Contactus clas)

    {
        string Sname = clas.Name;
        string Smobile = clas.Mobile;
        string Semail = clas.Email;
        string Scity = clas.City;
        string Sfeedback = clas.Feedback;
        TempData["Contactus"] = clas;
        return RedirectToAction("Select");
    }
    public ActionResult Select()
    {
       ViewBag.Message = "Your Saved page.";
       var Saved = TempData["Contactus"] as Models.Contactus;
        if (Saved == null)
        {
            Saved = new Models.Contactus();
        }

        return View("Select",Saved);
    }

THIS is MY Contact VIEW
HTML
@model WebApplication2.Models.Contactus
@{
    ViewBag.Title = "Contact";
}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>

@using (Html.BeginForm())
{
    <div class="form-group">
        <label class="control-label col-lg-2" for="name">Name<span class="required">*</span> </label>
        @Html.TextBoxFor(x => x.Name)
    </div>
    <div class="form-group">
        <label class="control-label col-lg-2" for="email">Email<span class="required">*</span> </label>
        @Html.TextBoxFor(x => x.Email)
    </div>
    <div class="form-group">
        <label class="control-label col-lg-2" for="contactnum">Contact Number<span class="required">*</span> </label>
        @Html.TextBoxFor(x => x.Mobile)
    </div>
    <div class="form-group">
        <label class="control-label col-lg-2" for="City">City<span class="required">*</span> </label>
        @Html.TextBoxFor(x => x.City)
    </div>
    <div class="form-group">
        <label class="control-label col-lg-2" for="Feedback">Comment<span class="required">*</span> </label>
        @Html.TextAreaFor(x => x.Feedback)
    </div>
        <input type="submit" value="Contact" class="btn-primary" />
       
}

This is my save view
Razor
 @{
    ViewBag.Title = "Data";
    }
    <h2>Data Saved</h2>

 
    @using (Html.BeginForm())
    {
}

THIS iS MY MODEL
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication2.Models
{
    public class Contactus
    {
       public string Name { get; set;}
       public string Feedback { get; set; }
       public string Mobile { get; set; }
       public string Email { get; set; }
       public string City { get; set; }
    }
}
Posted
Comments
Sreekanth Mothukuru 15-Jul-15 3:29am    
Have you tried TempData ?
Member 11698441 15-Jul-15 23:57pm    
Yes I used tempdata in controller.

1 solution

hello

I've tried out your code and it seems to work. The only problem I came across is it skipping the Contact view and going straight to the Select view. An easy fix, just make your current Action Method for Contact HttpPost and add a new Contact Action Method that just returns the view which will be your HttpGet.

CSS
[HttpGet]
public ActionResult Contact()
{
    return View();
}

[HttpPost]
public ActionResult Contact(Models.Contactus clas)
{
    string Sname = clas.Name;
    string Smobile = clas.Mobile;
    string Semail = clas.Email;
    string Scity = clas.City;
    string Sfeedback = clas.Feedback;
    TempData["Contactus"] = clas;
    return RedirectToAction("Select");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900