简体   繁体   中英

transferring the return value from actionresult to another view

I am trying to show the data in a label inside view, the data is coming from another view for that purpose i have did like this

this is my model is

namespace MvcSampleApplication.Models
{
    public class products
    {
        [Required]
        [Display(Name = "Textbox1")]
        public string EnteredValue { get; set; }
    }    
    public class category
    {
        [Display(Name = "Label1")]
        public string lablvalue { get; set; }        
    }
}

and this is my controller

namespace MvcSampleApplication.Controllers
{
    public class EnterValueController : Controller
    {           
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Valuesadd(MvcSampleApplication.Models.products model)
        {
            return View(model);            
        }
        [HttpPost]
        public ActionResult SubmitValue(MvcSampleApplication.Models.products model)
        {
            ViewData["logindata"] = model.EnteredValue;    
            return View("submittedvalues");               
        }                
        public ActionResult submittedvalues()
        {
            var model = new MvcSampleApplication.Models.category();
            string data = ViewData["logindata"] != null ? ViewData["logindata"].ToString() : "";
            model.lablvalue = data;
            return View(model);            
        }
    }
}

and this my view

@model MvcSampleApplication.Models.category
@{
    ViewBag.Title = "submittedvalues";
}
<h2>submittedvalues</h2>
@using (Html.BeginForm())
{
    <div>
        <fieldset>
          <legend>
              Lable value
          </legend>                
            @Html.DisplayFor(s=>s.lablvalue)         
        </fieldset>
    </div>
} 

when i press the submit button it will be going to view(submittedvalues.cshtml) but not displaying results Many thanks....

Change your code as, Instead of return View() use RedirectToAction to redirect it to another action. Also notice that I have used TempData instead of ViewData

[HttpPost]
public ActionResult SubmitValue(MvcSampleApplication.Models.products model)
{
    TempData["logindata"] = model.EnteredValue;    
    return RedirectToAction("submittedvalues");               
}                
public ActionResult submittedvalues()
{
    var model = new MvcSampleApplication.Models.category();
    string data = string.IsNullOrEmpty(TempData["logindata"]) ? string.Empty : TempData["logindata"].ToString();
    model.lablvalue = data;
    return View(model);            
}

That's because this:

return View("submittedvalues"); 

immediately renders the view called "submittedvalues.cshtml"

So this code:

public ActionResult submittedvalues()
        {
            var model = new MvcSampleApplication.Models.category();
            string data = ViewData["logindata"] != null ? ViewData["logindata"].ToString() : "";
            model.lablvalue = data;
            return View(model);            
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM