ARTICLE AD BOX
I'm new to ASP.NET MVC and want to create a web-based recipe app. In this app, I want to read XML files from my computer or mobile. For that reason, I want to copy the file location into a text file and reading the file after clicking a submit button.
My index.cshtml is:
@model IEnumerable<Beer> @{ ViewData["Title"] = "Index"; } <h1>Index</h1> <p> <a asp-action="Create">Create New</a> <div class="form-group"> <label>XML Bestand:</label> <input type="text" size="100" name="Location" id="Location" asp-page-handler="Custom1" value="D:\OneDrive\De Roode Toren\gildewedstrijd\Rye-Rasp Taz 1.xml" asp-action="Bestandlocatie" /> </div> <div class="form-group"> <a asp-action="CreateFromXML" id="Location" > Create From XML </a> </div> </p> <table class="table"> <thead> <tr> <th> @Html.DisplayNameFor(model => model.BeerName) </th> <th> @Html.DisplayNameFor(model => model.BrewDate) </th> <th> @Html.DisplayNameFor(model => model.BeerType) </th> <th> @Html.DisplayNameFor(model => model.Kooktijd) </th> <th> @Html.DisplayNameFor(model => model.GildelidId) </th> <th> @Html.DisplayNameFor(model => model.GildeLid) </th> <th> @Html.DisplayNameFor(model => model.OG) </th> <th> @Html.DisplayNameFor(model => model.FG) </th> <th> @Html.DisplayNameFor(model => model.ABV) </th> <th> @Html.DisplayNameFor(model => model.IBU) </th> <th></th> </tr> </thead> <tbody> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.BeerName) </td> <td> @Html.DisplayFor(modelItem => item.BrewDate) </td> <td> @Html.DisplayFor(modelItem => item.BeerType) </td> <td> @Html.DisplayFor(modelItem => item.Kooktijd) </td> <td> @Html.DisplayFor(modelItem => item.GildelidId) </td> <td> @Html.DisplayFor(modelItem => item.GildeLid) </td> <td> @Html.DisplayFor(modelItem => item.OG) </td> <td> @Html.DisplayFor(modelItem => item.FG) </td> <td> @Html.DisplayFor(modelItem => item.ABV) </td> <td> @Html.DisplayFor(modelItem => item.IBU) </td> <td> <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> | <a asp-action="Details" asp-route-id="@item.Id">Details</a> | <a asp-action="Delete" asp-route-id="@item.Id">Delete</a> </td> </tr> } </tbody> </table>My inputbox has the name and id Locatie. With the asp-action="CreateFromXML", I want to create a new recipe from this XML file.
In my controller, I have this code:
//GET:Beers/CreateFromXML public IActionResult CreateFromXML(IFormCollection form) { var yourValue = $('#Locatie').val(); //var value = collection["XMLbestand"]; string Name = form["Name"]; //Viewbag.Name = Name; var filename = "D:\\OneDrive\\De Roode Toren\\gildewedstrijd\\Rye-Rasp Taz 1.xml"; Bestandlocatie loc = new Bestandlocatie() { Locatie = "XMLbestand" }; XmlDocument doc = new XmlDocument(); doc.Load(filename); List<Beer> newbeers = new List<Beer>(); string name = ""; DateTime date = DateTime.Now; string brewer = ""; decimal abv = 0; decimal og = 0; decimal fg = 0; int ibu = 0; int boiltime = 0; string style = ""; foreach (XmlNode node in doc.DocumentElement.ChildNodes) { foreach (XmlNode node1 in node) { DbContextOptions<BierenProevenContext> options = new DbContextOptions<BierenProevenContext>(); if (node1.Name == "NAME") name = node1.InnerText; if (node1.Name == "DATE") date = DateTime.Parse(node1.InnerText); if (node1.Name == "BREWER") brewer = node1.InnerText; if (node1.Name == "EST_ABV") abv = decimal.Parse(node1.InnerText.Replace(".", ",")); if (node1.Name == "IBU") ibu = (int)decimal.Parse(node1.InnerText.Replace(".", ",")); if (node1.Name == "EST_OG") og = decimal.Parse(node1.InnerText.Replace(".", ",")); if (node1.Name == "EST_FG") fg = decimal.Parse(node1.InnerText.Replace(".", ",")); if (node1.Name == "BOIL_TIME") ibu = (int)decimal.Parse(node1.InnerText.Replace(".", ",")); if (node1.Name == "STYLE") { foreach (XmlNode node2 in node1) { if (node2.Name == "NAME") style = node2.InnerText; if (node2.Name == "STYLE_LETTER") style += " (" + node2.InnerText + ")"; } } } } Beer beer = new Beer() { BeerName = name, ABV = abv, BeerType = style, BrewDate = date, GildeLid = brewer, GildelidId = 4, FG = (decimal?)fg, OG = (decimal?)og, Kooktijd = boiltime, IBU = ibu, }; return View(beer); }The code is working, but I have to use the file location in the code. How can I get the file location from my textbox into the code?
