【问题标题】:Model not updating after Post发布后模型未更新
【发布时间】:2014-07-28 03:49:44
【问题描述】:

我正在尝试拖放以上传图片,然后在裁剪同一张图片之后。

到目前为止,我有一个名为 Index 的页面,它可以毫无问题地上传图片,但发布后它没有更新页面上的模型。我正在使用 Html.helpers 来显示模型中的数据。我试过清除模型状态,但没有任何变化。我已经使用 dropzone.js 进行上传和裁剪,我已经准备好 Jcrop。

型号:

    public class ImageModel
{
    public string ImageUrl { get; set; }
    public string FileName { get; set; }
}

public class EditorInputModel
{
    public ImageModel Image { get; set; }
    public double Top { get; set; }
    public double Bottom { get; set; }
    public double Left { get; set; }
    public double Right { get; set; }
    public double Width { get; set; }
    public double Height { get; set; }

    public string Caption { get; set; }
}

行动:

 public ActionResult Index()
    {
        var model = new EditorInputModel();
        model.Image = new ImageModel();
        model.Image.ImageUrl = "";
        model.Image.FileName = "";
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(EditorInputModel model)
    {

        var image = WebImage.GetImageFromRequest();
        if (image != null)
        {
            var filename = Guid.NewGuid().ToString("d");

            model.Image = new ImageModel();                
            model.Image.FileName = image.FileName;                
            model.Image.ImageUrl = string.Format("data:image/{0};base64,{1}",image.ImageFormat, Convert.ToBase64String(image.GetBytes()));


            model.Width = image.Width;
            model.Height = image.Height;
            model.Top = image.Height * 0.1;
            model.Left = image.Width * 0.9;
            model.Right = image.Width * 0.9;
            model.Bottom = image.Height * 0.9;
            ModelState.Clear();
        }
        return View(model);
    }

查看:

@model FileUploadTest1.Controllers.EditorInputModel

@{
    ViewBag.Title = "Index";
}
<h2>File Upload Test</h2>

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data", @class = "dropzone", id = "dropzoneForm" }))
{

}

@using (Html.BeginForm("Editor","Home", FormMethod.Post, new { enctype = "multipart/form-data", id = "cropView", style = "display:none" }))
{    <div class="container">
        <div class="image-cropper">
        @Html.HiddenFor(x => x.Left)
        @Html.HiddenFor(x => x.Right)
        @Html.HiddenFor(x => x.Top)
        @Html.HiddenFor(x => x.Bottom)
        @Html.HiddenFor(x => x.Image.ImageUrl)
        @Html.HiddenFor(x => x.Image.FileName)

        @Model.Left;
        <div class="col-lg-7 col-md-5 col-sm-7">
            <img src="@Model.Image.ImageUrl" runat="server" class="image " style="max-width:400px;" alt="" />
        </div>
        <div class="col-lg-1 col-md-2 col-sm-1"></div>
        <div class="col-lg-4 col-md-5 col-sm-4">
            <div style="margin: 50px; width: 200px; text-align: center">
                <div class="preview" style="width: 200px; height: 200px">
                </div>
                <input type="checkbox" class="keepAspectRatio" />
                Lock aspect ratio<br />
            </div>
        </div>
    </div>
</div>
<div class="container">
    <br />
    <div class="col-lg-12 col-md-12 col-sm-12">
        @Html.LabelFor(model => model.Caption)
        @Html.TextBoxFor(model => model.Caption, new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.Caption)
        <div class="container center">
            <br />
            <input type="submit" name="action" class="btn btn-success" value="Crop" />
        </div>
    </div>
</div>

}

【问题讨论】:

    标签: image post model-view-controller dropzone.js modelstate


    【解决方案1】:

    您正在视图中调用 controller.classname

      @model FileUploadTest1.Controllers.EditorInputModel
    

    必须是model.classname

      @model projectname.Models.classname
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      • 2015-12-25
      • 2013-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多