1

I have site - user choose option from dropdown, depends oh his select site must to be generated. I use JS and blazor. I have 404 error. I tried to adapted this way: Populate DropDownList from another DropDownList

View:

@{
Layout = "~/Views/Shared/_Layout.cshtml";
 }
 @model IEnumerable<Article>
 @section Scripts{

<script>
    function load() {
        var e = document.getElementById('titles');
        var id = e.options[e.selectedIndex].value;
        var Id = parseInt(id);
        //C: \Users\patryk\source\repos\Blog\Controllers\Dashboard\EditPartialViewController.cs
        alert(Id);
        $('#container').load("/Dashboard/EditPartialViewController/_tableSeeder?id="+Id);


    }
</script>
 }

 <h1>Edit</h1>
 <form id="id_select" >
<label for="article">Choose an article to edit:</label>
<select id="titles" onchange="load()">
    @foreach (var x in Model)
    {
        <option value=@x.Id>@x.Title</option>
    }
</select>
<br><br>

There is my partialView:

@model List<Article>

 @foreach (var item in Model)
 {
<option value="@item.Id">
    @item.Title
</option>
 }

Controller for partial view:

namespace Blog.Controllers.Dashboard
 {
public class EditPartialViewController : Controller
{
    private ApplicationDbContext context;
    public EditPartialViewController(ApplicationDbContext _context)
    {
        context = _context;
    } 
    public IActionResult _tableSeeder(int id)
    {
        List<Article> model = context.Articles.Where(x => x.Id == id).ToList();//list with only one           element
        return PartialView(model);
    }
}
 }

WebBrowser gives me 2 errors: GEThttps://localhost:44336/Dashboard/EditPartialViewController/_tableSeeder?id=14 [HTTP/2 404 Not Found 7ms]

and second one: XML error - main element not found (or sth like that)

EDIT: my routing:

 app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapRazorPages();

            endpoints.MapControllerRoute(
                name: "Dashboard",
                pattern: "{controller=Dashboard}/{action=Dashboard}");
            endpoints.MapRazorPages();
            //added for block registration
            endpoints.MapGet("/Identity/Account/Register", context => Task.Factory.StartNew(() => context.Response.Redirect("/Identity/Account/Login", true, true)));
            endpoints.MapPost("/Identity/Account/Register", context => Task.Factory.StartNew(() => context.Response.Redirect("/Identity/Account/Login", true, true)));
        });
  • \@model and \@sections say this is not Blazor. – H H Oct 03 '20 at 05:08
  • `/Dashboard/EditPartialViewController/_tableSeeder?` : it's not clear how you get /Dashboard/ but normally `Controller` is dropped: `/Dashboard/EditPartialView/_tableSeeder?` – H H Oct 03 '20 at 05:09
  • @HenkHolterman So, what is it? If I drop Controller it doesn;t work as well. –  Oct 03 '20 at 20:08
  • You have a route/url mismatch. You will have to learn to debug that. Step 1: reconstruct the url for the API and call it with a Browser or PostMan. Figure out the routing setup in your server, you didn't post any of that. – H H Oct 03 '20 at 20:35
  • @HenkHolterman I have no API. It's very simple app. In folder Controllers I have controllers and Dashboard folder with EditPartialViewController. –  Oct 03 '20 at 21:21
  • @HenkHolterman Check my edit, I added my routing but i don;t know why you want it. –  Oct 03 '20 at 21:45
  • _tableSeeder is an API alright. I think you should lose `/Dashboard` in `"/Dashboard/EditPartialView/_tableSeeder?id="+Id` but as I said, learn to debug this. – H H Oct 03 '20 at 21:52
  • @HenkHolterman Thx! It works in little part ;) Now my method gives bad Id of article. I think my routing is bad, but I don;t know how to fix it. It always gets "0" (id) –  Oct 04 '20 at 20:42
  • 1
    I think you need `"/EditPartialView/_tableSeeder/"+Id` – H H Oct 04 '20 at 20:54
  • @HenkHolterman Yes! Now it works! You helped me a lot of! Thank you very,very much! –  Oct 04 '20 at 21:33

0 Answers0