D
D
Dmitry Fondomakin2019-06-28 13:46:15
ASP.NET
Dmitry Fondomakin, 2019-06-28 13:46:15

MVC how to output modified text to HTML code from input on page reload?

Good afternoon.
Just started learning MVC. Mastering according to official documents and site metanit.com
HTML

@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
   <div class="form-group">
          <input type="email" class="form-control input-lg" id="exampleInputEmail2" placeholder="Your Email*">
   </div>
   <button type="submit" class="btn btn-info button">Подпишитесь</button>
   <p>@ViewData["Test"]</p>
}

HomeController
public IActionResult Index(string exampleInputEmail2)
        {
            ViewData["Test"] = exampleInputEmail2 + "_ТестТекст";
 
            return View();
        }

When the page is first loaded, the value is empty and nothing is displayed. After pressing the button, the page should be updated and the page should display an inscription from Input with changes.
Here I want to display text Can someone fix my code to a working state? And show how to use the method if it is needed here.
<p>@ViewData["Test"]</p>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Fondomakin, 2019-06-28
@defond

Model:

public class UserContainer
    {
        public string Fam { get; set; }
        public string Name { get; set; }
        public string Name2 { get; set; }
 
        public UserContainer(string F, string N, string N2)
        {
            Fam = F; Name = N; Name2 = N2;
        }
    }

Controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Vasya.Models;
 
namespace Vasya.Controllers
{
    public class HomeController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            UserContainer UCon = new UserContainer("","","");
            return View(UCon);
        }
 
        [HttpPost]
        public ActionResult Index(string Fam, string Name, string Name2)
        {
            UserContainer UCon = new UserContainer(Fam, Name, Name2);
            return View(UCon);
        }
 
    }
}

HTML5
@model System.Data.DataTable

<div>
        Привет, @Model.Fam &nbsp @Model.Name &nbsp @Model.Name2
       <form action="/Home/Index" method="post"> 
          <p><input class="text-box single-line" size = 20 id ="N1" name ="Fam" type ="text" value = "@Model.Fam" /> </p>
          <p><input class="text-box single-line" size = 20 id ="N2" name ="Name" type ="text" value = "@Model.Name" /> </p>
          <p><input class="text-box single-line" size = 20 id ="N3" name ="Name2" type ="text" value = "@Model.Name2" /> </p>
          <input type="submit" id="SendButton" value="Ok" />
       </form>
 
    </div>

I found a solution that might be useful to someone. The solution is working, everything works fine!
PS: Answering a question on the Toaster for yourself is, of course, strong. )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question