我有一个包含文件夹的数据库,我将整个路径存储到文件夹folder_name
所以......喜欢://MainFolder/subFolder/folderName
。在我的应用程序中,我有一个名为Folder
的模型,它代表db。
型号:
public class Folder
{
[Key]
public string folder_name { get; set; }
[NotMapped]
public string folder_name_short
{
get
{
string shortname = folder_name.Substring(folder_name.LastIndexOf("/"), folder_name.Length - folder_name.LastIndexOf("/")); //System.NullReferenceException here
return shortname;
}
set
{
string shortname = folder_name.Substring(folder_name.LastIndexOf("/"), folder_name.Length - folder_name.LastIndexOf("/"));
this.folder_name = folder_name.Replace(shortname, value);
}
}
}
folder_name_short
未映射,因为它只是整个路径中的子字符串,因此我不想将其存储两次。我希望我的意思是:
Console.WriteLine(folder_name) output://MainFolder/subFolder/folderName
Console.WriteLine(folder_name_short) output:/folderName
在我的视图中,用户可以重命名该文件夹。所以我想将旧的folder_name_short
替换为新的folder_name
并将新的字符串存储在.
.
using (Html.BeginForm("Rename", "Folders", FormMethod.Post))
{
@Html.EditorFor(model => model.folder_name_short)
<input type="submit" value="rename folder" id="submit" class="btn btn-default" />
}
.
.
查看:
folder_name_short
问题:
Inputtextbox呈现并显示其中System.NullReferenceException
的当前值。当我更改它并单击提交按钮时,我在模型中得到一个folder_name_short
(如源代码中所示,请滚动到右侧)。我不明白什么是错的,需要做哪些改变。
修改
当注释掉setter时,Exception会消失。那么设置者可能会导致错误吗?
解决方案:
使用标准的setter和getter来存储folder_name
值,但是实现一个公共的get / set方法来设置db中的[NotMapped]
public string folder_name_short { get; set; }
public string getfolder_name_short()
{
string shortname = folder_name.Substring(folder_name.LastIndexOf("/"), folder_name.Length - folder_name.LastIndexOf("/"));
return shortname;
}
public void setfolder_name_short(string newname)
{
string shortname = folder_name.Substring(folder_name.LastIndexOf("/"), folder_name.Length - folder_name.LastIndexOf("/"));
folder_name = folder_name.Replace(shortname, newname);
}
并在控制器中调用此方法。所以:
__global__ void childKernel()
{
printf("Hello ");
}
__global__ void parentKernel()
{
childKernel<<<1,5>>>();
cudaDeviceSynchronize();
printf("World!\n");
}
答案 0 :(得分:1)
如果在表单上编辑的值是短名称folder_name_short
,那么模型类可以包含其字符串属性:
public string folder_name_short { get; set; }
此属性不包含任何逻辑。包含某些逻辑的任何其他代码都可以在操作方法中位于控制器中。
您似乎使用相同的类进行数据库访问,并将其作为MVC视图的模型类,这是问题的根源。
答案 1 :(得分:0)
[HttpPost]
[("Rename")]
public async Task<IHttpActionResult> ChangeFolderPath(Folder folder)
{
//Put a breakpoint here and make sure the folder has a value
}
可能是setter方法覆盖了folder_name的值。
我建议这个帖子来帮助理解自定义setter C# properties: how to use custom set property without private field?
修改
public class Folder
{
[Key]
public string folder_name { get; set; }
/*Model Logic shouldn't be in the Poco*/
/*You may put that in your model or best thing would be to implement a repository pattern*/
public string ShortName()
{
string shortname = folder_name.Substring(folder_name.LastIndexOf("/"), folder_name.Length - folder_name.LastIndexOf("/")); //System.NullReferenceException here
return shortname;
}
public void SetUsingShortName(string value)
{
string shortname = folder_name.Substring(folder_name.LastIndexOf("/"), folder_name.Length - folder_name.LastIndexOf("/"));
this.folder_name = folder_name.Replace(shortname, value);
}
}