C# 编辑方法asp.net

C# 编辑方法asp.net,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我正在尝试为用户做一个编辑页面,用户可以编辑用户名和电子邮件。 我的控制器如下所示: public ActionResult Edit(string Name="0") { using (var applicationContext = new ApplicationContext()) { var User = applicationContext.ApplicationUsers.Where(s => s.Name ==

我正在尝试为用户做一个编辑页面,用户可以编辑用户名和电子邮件。 我的控制器如下所示:

 public ActionResult Edit(string Name="0")
    {
        using (var applicationContext = new ApplicationContext())
        {
            var User = applicationContext.ApplicationUsers.Where(s => s.Name == Name).SingleOrDefault();
            if (User == null)
            {
                return NotFound();
            }
            return View(User);
        }
    }


 [HttpPost, ActionName("Edit")]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> EditPost(ApplicationUser user)
    {
        if (Name == null)
        {
            return NotFound();
        }
        using (var applicationContext = new ApplicationContext())
        {
            var UserUpdate = await applicationContext.ApplicationUsers.SingleOrDefaultAsync(s => s.Name == Name);
            if (await TryUpdateModelAsync<ApplicationUser>(UserUpdate, "", s => s.Name, s => s.Email))
            {
                try
                {
                    await applicationContext.SaveChangesAsync();
                    return RedirectToAction(nameof(Index));
                }
                catch (DbUpdateException /* ex */)
                {
                    //Log the error (uncomment ex variable name and write a log.)
                    ModelState.AddModelError("", "Unable to save changes. " +"Try again, and if the problem persists, " +"see your system administrator.");
                }
            }
            return View(UserUpdate);
        }
    }
公共操作结果编辑(string Name=“0”) { 使用(var applicationContext=new applicationContext()) { var User=applicationContext.ApplicationUsers.Where(s=>s.Name==Name.SingleOrDefault(); if(User==null) { 返回NotFound(); } 返回视图(用户); } } [HttpPost,ActionName(“编辑”)] [ValidateAntiForgeryToken] 公共异步任务EditPost(ApplicationUser用户) { if(Name==null) { 返回NotFound(); } 使用(var applicationContext=new applicationContext()) { var UserUpdate=wait applicationContext.ApplicationUsers.SingleOrDefaultAsync(s=>s.Name==Name); if(等待tryupdatemodelsync(UserUpdate,“,s=>s.Name,s=>s.Email)) { 尝试 { 等待applicationContext.saveChangesSync(); 返回重定向到操作(名称(索引)); } catch(DbUpdateException/*ex*/) { //记录错误(取消注释ex变量名并写入日志。) AddModelError(“,”无法保存更改。“+”请重试,如果问题仍然存在,“+”请与系统管理员联系。”); } } 返回视图(UserUpdate); } } 这也是我的模型:

 public class ApplicationUser : BaseEntity
{
    public string Name { get; set; }
    public string DirectoryId { get; set; }
    public string Domain { get; set; }
    public bool IsActive { get; set; }
    public List<ApplicationUser> AllUsers { get; set; }

    public ApplicationUser()
    {
        AllUsers = new List<ApplicationUser>();
    }
}
公共类应用程序用户:BaseEntity
{
公共字符串名称{get;set;}
公共字符串DirectoryId{get;set;}
公共字符串域{get;set;}
公共bool IsActive{get;set;}
公共列表{get;set;}
公共应用程序用户()
{
AllUsers=新列表();
}
}

有人能帮我吗我不明白这里出了什么问题,我在编辑时遇到了一些问题,因为我总是用Int-Id而不是String-Id来编辑。。对我来说很奇怪有人能帮我吗

正如我在评论中所说,在
ActionResult Edit(string Name=“0”)
中,给出主键的正确名称,然后根据该名称筛选/获取对象,例如:
(var UserUpdate=wait applicationContext.ApplicationUsers.SingleOrDefaultAsync(s=>s.PrimaryKeyPropertyName==user.PrimaryKeyPropertyName);)

如果您收到一个对象
ApplicationUser
,并假设该对象是您视图的模型,并且您有一个属性
Id
,它将类似于:

public ActionResult Edit(int Id)
{
    using (var applicationContext = new ApplicationContext())
    {
        var User = applicationContext.ApplicationUsers.Where(s => s.Id ==  Id).SingleOrDefault();
        if (User == null)
        {
            return NotFound();
        }
        return View(User);
    }
}


[HttpPost, ActionName("Edit")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> EditPost(ApplicationUser user)
{
    //Here will return every time NotFound/error, right? Where you defined Name?!
    /*if (Name == null)
    {
        return NotFound();
    }*/
    using (var applicationContext = new ApplicationContext())
    {
        var UserUpdate = await applicationContext.ApplicationUsers.SingleOrDefaultAsync(s => s.Id == user.Id);

...
public ActionResult编辑(int-Id)
{
使用(var applicationContext=new applicationContext())
{
var User=applicationContext.ApplicationUsers.Where(s=>s.Id==Id.SingleOrDefault();
if(User==null)
{
返回NotFound();
}
返回视图(用户);
}
}
[HttpPost,ActionName(“编辑”)]
[ValidateAntiForgeryToken]
公共异步任务EditPost(ApplicationUser用户)
{
//这里将返回每次NotFound/错误,对吗?在哪里定义名称?!
/*if(Name==null)
{
返回NotFound();
}*/
使用(var applicationContext=new applicationContext())
{
var UserUpdate=wait applicationContext.ApplicationUsers.SingleOrDefaultAsync(s=>s.Id==user.Id);
...

首先,您的POST方法参数需要是您的模型,而不是
string
@StephenMuecke已经修复了“我不明白”的问题这不是一个特别有用的描述问题。考虑这是一个熟悉一些基本调试的机会。当你在调试器中通过这个代码时,具体会发生什么?你希望发生什么?为什么?@ VolminAzeiteiro:“这对我来说毫无意义”。也不能有效地描述实际问题。如果此代码没有以任何特定方式出现故障,那么我们也无法为您提供太多帮助。听起来您需要的只是更多的教程和练习。例如,
int
string
之间的差异可能是多种多样的基本上,
int
是一个数值,
string
是一个文本值(可能是数字,也可能不是数字,没关系)。@VolminAzeiteiro:“我的问题在于查找”-也许你可以详细说明这个问题是什么?试着理解我们从这里看不到你的屏幕。我们不知道你有什么问题。这里还值得指出的是,问题中显示的代码没有使用任何
.Find()
method。那么,再说一遍……您遇到了什么问题?请花点时间描述一下问题。第5行的错误,其中“s.Id==Id”错误是“无法将int类型隐式转换为stringChange:
ActionResult Edit(string Id)
ApplicationUsers.where(s=>s.Id.Equals(Id))
get方法为空,但当我键入url时,edit/1986a3ef-e032-44db-bc37-3b5eb0737408会显示需要编辑的配置文件