变量(var)从控制器到视图mvc 5 [英] variable (var) from controller to view mvc 5

查看:67
本文介绍了变量(var)从控制器到视图mvc 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究MVC 5我只需要知道变量是否可以从控制器传递到视图?如果是这样应该怎么做?我想将值从一个视图(文本框)复制到另一个视图(标签),并从文本框显示到标签,请帮助。



这是我的控制器

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

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

@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

 @{
    ViewBag.Title = "Data";
    }
    <h2>Data Saved</h2>

 
    @using (Html.BeginForm())
    {
}



这是我的模型


THIS iS MY MODEL

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; }
    }
}

推荐答案

你好



我已经尝试了你的代码,它似乎工作。我遇到的唯一问题是它跳过Contact视图并直接进入Select视图。一个简单的解决方法,只需制作您当前的联系HttpPost操作方法,并添加一个新的联系方式,该方法只返回您的HttpGet视图。



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.

[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");


这篇关于变量(var)从控制器到视图mvc 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆