Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 向MVC文件添加更多数据_Asp.net Core_Iformfile - Fatal编程技术网

Asp.net core 向MVC文件添加更多数据

Asp.net core 向MVC文件添加更多数据,asp.net-core,iformfile,Asp.net Core,Iformfile,如何向.net core中的文件字段添加更多详细信息 例如,我需要将特定属性附加到 提交文件时如何读取属性myproperty,因为我在服务器端捕获文件 包含文件名和输入名称的IFormFile请确保使用asp.net core 3.x。由于,asp.net core 2.2无法接收服务器端模型中的IFormFile 型号: public class FileInfo { public string FileName { get; set; } public string In

如何向.net core中的文件字段添加更多详细信息

例如,我需要将特定属性附加到

提交文件时如何读取属性myproperty,因为我在服务器端捕获文件


包含文件名和输入名称的IFormFile

请确保使用asp.net core 3.x。由于,asp.net core 2.2无法接收服务器端模型中的IFormFile

型号:

public class FileInfo
{
    public string FileName { get; set; }
    public string InputName { get; set; }
    public IFormFile File { get; set; }
}
public class FileInfo
{
    public int Id { get; set; }
    public string FileName { get; set; }
    public string InputName { get; set; }
    public IFormFile File { get; set; }
}
public class Test
{
    public int Id { get; set; }
    public string Name { get; set; }
    public FileInfo FileInfo { get; set; }
}
视图:

视图:

@模型测试
控制器:

public class FileInfoController : Controller
{
    public IActionResult Create()
    {
        return View();
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create(FileInfo fileInfo)
    {
        if (ModelState.IsValid)
        {
            //save file info..
        }
        return View(fileInfo);
    }
}
 [HttpPost]
 public async Task<IActionResult> Create(Test test) 
 {
     if (ModelState.IsValid)
     {
         //save file info..
     }
     return View(test);
 }
[HttpPost]
公共异步任务创建(测试)
{
if(ModelState.IsValid)
{
//保存文件信息。。
}
返回视图(测试);
}
结果:

可能是重复的,不,这里不重复,提交数据与IFormFile对象分开,我的问题与此类似,作为此问题的包装,而不使用.net core 3。我没有直接在模型中使用它我在包装类中使用它你的asp.net core版本是什么?我已经尝试了asp.net core 2.2。它可以很好地与嵌套模型一起工作。请检查。如果它对你有帮助,你能接受作为答案吗?请参阅:。如果仍然有问题,请跟进,让我知道。
@model Test
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create" enctype="multipart/form-data">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Name" class="control-label"></label>
                <input asp-for="Name" class="form-control" />
                <span asp-validation-for="Name" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="FileInfo.FileName" class="control-label"></label>
                <input asp-for="FileInfo.FileName" class="form-control" />
                <span asp-validation-for="FileInfo.FileName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="FileInfo.InputName" class="control-label"></label>
                <input asp-for="FileInfo.InputName" class="form-control" />
                <span asp-validation-for="FileInfo.InputName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="FileInfo.File" class="control-label"></label>
                <input asp-for="FileInfo.File" value="Upload">
                <span asp-validation-for="FileInfo.File" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>
 [HttpPost]
 public async Task<IActionResult> Create(Test test) 
 {
     if (ModelState.IsValid)
     {
         //save file info..
     }
     return View(test);
 }