English 中文(简体)
ASP.NET MVC核心网络评估,从局部角度更新我的数据库
原标题:ASP.NET Core MVC web app, updating my database from partial view

我试图在用户指挥数据库中更新记录,我想知道这样做的最佳办法。 我猜测,我缺乏正确的术语来研究我应当使用的适当工具和模式。

<>1>

我开始试图撰写一份可以称之为“快车牌”功能的 Java字功能,但我怀疑,Im离开了产生伙伴关系的许多权力和功能。 NET 核心应用。

<>2>

拉扎尔的网页本应取代ASP。 NET 因此,在MVC系统中,在为返还观点组成部分而设置的对依赖性注射的同时,似乎也多余。 因此,我认为,必须找到办法,通过平坐页而不是按照模型、观点、控制器夹对项目进行泥.。

该项目与伙伴关系项目相同。 除在没有boot锁的情况下重建外,在甚小区建立了网络核心模板。

因此,它有一个<代码>_layout.cshtml,其中部分使用使用户菜单。

If it helps, the website is studiobreifcase.com, and public in my git repo.

https://github.com/TAReber/StudioBriefcase-Web-App

你们可以打着它,管理它,但我不敢肯定什么工作,什么是徒劳的。

目前的目标只是将产出记录在青春期。

<代码>_menu Partial.cshtml, 载于_layout.cshtml:

@{
}

    // ... irrelevant code

    @if (Context.User.Identity?.IsAuthenticated == true)
    {
        // ...
        <select id="Menu_RoleDropdown" onchange="updateUserRole()">
            <option value="Default">Anonymous</option>
            <option value="Regular">Regular</option>
            // ...
        </select>
    }
    else 
    { 
        // OAuth Login stuff
        // irrelevant code
        ...
    }

    <script>
            async function updateUserRole() {
                var selectedRole = document.getElementById("Menu_RoleDropdown").value;
                console.log(selectedRole);

                // Attempted to access a function in UserService.cs but failed.            
            }
   </script>

www.un.org/Depts/DGACM/index_spanish.htm

namespace StudioBriefcase.Services
{
    public class UserService : IUserService
    {
        // ... Members, Constructor and other methods.

        // Function  I failed to call
        public Task SetUserRole(string role)
        {
            _logger.LogError($"Setting user Role to {role}");
            throw new NotImplementedException();
        }
    }
}

我只想在这一尝试中产生原木产出。 我没有成功。 这可能是好的,因为我不知道我做些什么,而且我不得不第一次做坏事。

我猜测这个问题是很宽泛的,但任何帮助都有助于了解我应该/应该利用哪些模式和做法来与安森系统连接。

问题回答

我花了几个星期做另一个项目。 但是,当我回过头来时,它就清除了我的头盔。 我试图追随Jalpa的反应。

我没有像他一样,设立了一个用户主计长,由Ajax连接,用户主计长在用户服务中称职。

我不知道这是否更好,但我认为,这会使整个用户服务机构不成为APIC的一部分。

我绘制了节目中控制器路线的地图,该路线最小。

app.MapControllerRoute(
name: "api",
pattern: "api/{controller}/{action}/{id?}");

我对我的申请是安全的感到很遗憾。 我不知道它应该怎样看待。 我认为,我至少可以增加检查,帮助我发现可疑行为。

namespace StudioBriefcase.Controllers
{
[ApiController]
[Route("api/user")]
public class UserController : Controller
{
    private readonly UserService _userService;

    public UserController(UserService userService)
    {
        _userService = userService;
    }

    //TODO:: Learn how to use ILogger and come up with some standardized system to log errors
    [HttpPost("UpdateUserClass")]
    public async Task<IActionResult> UpdateUserClass([FromBody] UserRoleUpdateModel model)
    {

        if (User.Identity != null && User.Identity.IsAuthenticated)
        {
            if(model.Id == User.FindFirst("sub")?.Value)
            {
                if (uint.TryParse(model.Id, out uint id))
                {
                    //Console.WriteLine(id);
                    await _userService.SetUserClass(id, model.userclass);
                    return Ok(new { Message = "Successfully Updated role to ", model.userclass });
                }
                else
                {
                    return Ok(new { Message = "Failed to Change Role" });
                }
            }
            else
            {
                return Ok(new { Message = "Wrong User ID Submitted, Is this a Hack Attempt?" });
            }
        }
        return Ok(new { Message = "User Identity was Null, UserController.UpdateUserClass Method" });
    }

}
}

我原谅,但我第一次使用手提式操作,是一些测试代码,我写道,从你管中删除了公共数据。 这有助于我了解我正在做些什么。 我可以用一个 but子或一个东西来形容这一 j。

<script>
function updateUserRole(event) {
    event.stopPropagation();
    
    var selectedRole = document.getElementById( Menu_RoleDropdown ).value;
    var userId =  @User.FindFirst("sub")?.Value ;

    fetch( /api/user/UpdateUserClass , {
        method:  POST ,
        headers: {
             Content-Type :  application/json 
        },
        body: JSON.stringify({
            Id: userId,
            userClass: selectedRole
        })
    })
        .then(response => {
            if (!response.ok) {
                throw new Error("Network response was not ok");
            }
            return response.json();
        })
        .then(data => {
            console.log(data);
        })
        .catch(error => {
            console.error("Fetch Operation failed in _menuPartial.cshtml:", error);
        });
}

感谢所有发表意见的人。 这非常有益。 录像带。 我不知道我的法典是多么好。 我的猜测是4个月,从现在起,我就认识到,我需要致力于另一个大项目。

改进伐木,改进数据库管理,改进系统。





相关问题
.NET 7 minimal API request timeout

I found this in .NET 8 (prerelease) but I can t use it due to company policy. https://learn.microsoft.com/en-us/aspnet/core/performance/timeouts?view=aspnetcore-8.0&viewFallbackFrom=aspnetcore-7.0 ...

Blazor web assembly pass checkbox list values to model

I m new to Blazor. I m working in a web assembly Blazor project. I am trying to create a form that passes values back to a model. I have it working with input text fields and drop downs, but I am ...

asp.net 6 c# mvc one to many get data from many side

I am trying to access the data on the many side of a one to many relationship. In the Index action of my controller I assign the data to a variable and return it. I have a Blog, the one side, with ...

How to update shared view adminheader.cshtml from a model

I ve added a drop down into my shared adminheader.cshtl view and it needs to be dynamically updated from data from a table. So when I use another view like routeplan.cshtml I load the view from ...

Gitlab CI On Prem, Docker Image and ASP.NET Core 7

We have a .NET 6 application. We added CI using: image: mcr.microsoft.com/dotnet/sdk:6.0 before_script: - dotnet restore --packages $NUGET_PACKAGES_DIRECTORY build: stage: build script: - ...

ASP.NET Core 2 .1 - How to show all data in table

I don t know how to put CustomerData into CustomerLists property. I m already using CustomerLists = CustomerData; but I got error: CustomerLists is a type but is used like a variable Can anyone ...

热门标签