c# - 返回模型但它为空

标签 c# asp.net-mvc-4 model imageresizer

我正在使用 Imageresizer 并上传照片。我想获取收到的 URL 并将其返回到我的 View 。我有我想要的 URL,但我无法进一步返回它...这是我目前的代码:

public ActionResult UploadPhoto(Image img, HttpPostedFileBase file, ProfilePageModel model)
{
    var currentUser = CommunitySystem.CurrentContext.DefaultSecurity.CurrentUser;
    bool isAccepted = false;

    string fileName = string.Empty;
    if (file.ContentLength > 0)
    {
        string fName = System.Guid.NewGuid().ToString();
        // Generate each version
        foreach (string fileKey in System.Web.HttpContext.Current.Request.Files.Keys)
        {
            HttpPostedFile uploadFile = System.Web.HttpContext.Current.Request.Files[fileKey];
            // Generate a filename (GUIDs are best).
            fileName = Path.Combine("~/uploads/", fName + format);

            // Let the image builder add the correct extension based on the output file type
            ImageBuilder.Current.Build(uploadFile, fileName, new ResizeSettings(
                "width=300;format=jpg;mode=max;crop=20,20,80,80;cropxunits=100;cropyunits=100"));
            model.fileName = fileName;        
        }
        return RedirectToAction("EditProfile");
    }
    return RedirectToAction("EditProfile");
}

当我运行此命令“上传文件并按提交”时,我发现该模型为空,该模型包含有关个人资料的信息,例如姓名、您来自哪个国家/地区等。

我需要以某种方式向我的模型添加值,以便我可以返回它(这样它就不会崩溃)。有什么想法吗?

最佳答案

要使模型具有值,其属性需要存在于表单中。您至少需要为要通过 POST 发回的每个属性添加隐藏字段。

关于c# - 返回模型但它为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19931455/

相关文章:

ruby-on-rails - rails : Modifying a Model Generated by Scaffolding

c# - MVC 5 请求域 url

c# - wpf getter 和 setter

c# - 我如何为经典 ASP 创建一个不连续阻塞其他线程的 DOTNET COM 互操作程序集?

c# - .net 4.5中不存在使用System.Json的情况

asp.net-mvc-4 - ASP.net MVC4 路由

model-view-controller - Doctrine2(实体与存储库)

c# - 匿名类型出现在两个错误

asp.net-mvc-4 - SignalR 连接错误

Python Django - 模型 : for loop in query/filter