0

I am trying to use ASP.NET MVC to get user input via Html.BeginForm() and display what the user inputted on the same page/view. The issue im having is after the user hits submit, the data does not show.

Here's how my project is set up, In Index.cshtml:

@model SynNetTest.Models.HomeModel
@{
    ViewData["Title"] = "Home Page";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="text-center"
    <h2>The current directory: @Model.directory</h2>
    <h3>@Model.Load()</h3>

    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    {
        <table cellpadding="0" cellspacing="0">
            <tr>
                <th colspan="2" align="center">Word Details</th>
            </tr>
            <tr>
                <td>Enter Word: </td>
                <td>
                    @Html.TextBoxFor(m => m.word)
                </td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Submit" /></td>
            </tr>
        </table>
    }
    <h4>The word entered: @Model.word</h4>
</div>

In HomeModel.cs:

public class HomeModel
    {
        public string directory { get; set; }
        public WordNetEngine wordNet { get; set; }
        public string word { get; set; }
        public List<SynSet> synSetList { get; set; }

        //load words to access
        public string Load()
        {
            wordNet.AddDataSource(new StreamReader(Path.Combine(directory, "data.adj")), PartOfSpeech.Adjective);
      
            wordNet.Load();
            return "Database Loaded Successfully";

        }
    }

In HomeController.cs:

    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            var home = new HomeModel()
            {
                directory = Directory.GetCurrentDirectory(),
                wordNet = new WordNetEngine(),
            };
            return View(home);
        }


        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}

I am working with the Syn.WordNet NuGet package to try to get the definitions of words that the user enters to print out. So far, Im just trying to see if the input can be processed and put on the same page/view.

2 Answers 2

1

What I am understanding that you are not able to view form data in Home/Index method. You can add following code.

 [HttpPost]
 public IActionResult Index(HomeModel homeModel)
 {
            
 }

when user click on submit button it will bind all the view field details with HomeModel. In short you were missing method with [HttpPost] and model binding.

Also you can use FromForm attribute get individual field data. Hope it helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Adding this gives me an error in Index.cshtml saything that <h2>The current directory: @Model.directory</h2> is null
There should be two methods Index(HomeModel homeModel) which will have [HttpPost] the one i have mentioned in my code and another will be Index() the one you have mention in your code.
1

You don't even have the HttpPost method the form is posting back defined in your controller.

@model SynNetTest.Models.HomeModel
@{
    ViewData["Title"] = "Home Page";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="text-center"
    <h2>The current directory: @Model.directory</h2>
    <h3>@Model.Load()</h3>

    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    {
...

In the controller, you need to define HttpPost version of /home/index:

public class HomeController : Controller
{
    // By default, this is HttpGet
    public IActionResult Index()
    {
        var home = new HomeModel()
        {
            directory = Directory.GetCurrentDirectory(),
            wordNet = new WordNetEngine(),
        };
        return View(home);
    }

    [HttpPost]
    public IActionResult Index(HomeModel model)
    {
        // This is where you handle the form submit from the form post.
        ...
    }
...

By reading your code sample, I see other problems too. Please make sure to read the documentation from Microsoft first: https://learn.microsoft.com/en-us/aspnet/core/mvc/overview?view=aspnetcore-3.1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.