2013-05-30 38 views
-1

我正在开发asp.net mvc 3应用程序。我实现了一个剃须刀视图,它具有两个主要功能 - 基于数据库中的数据构建/显示表单,并在自定义(由我制作)图像库中显示与此表单相关的图像,该图像库允许上传和删除图片。根据控制器方法的执行结果在剃刀视图中显示消息

所以通常这是我与两种形式用于可视化的形式,并且显示和上传图像(多个)视图:

@model List<DataAccess.MCS_DocumentFields> 

@{ 
    ViewBag.Title = "Документ"; 
} 
<div id="alabala"> 
<div id="drawForm"> 
@using (Html.BeginForm("UpdateDocument", "Forms", FormMethod.Post)) 
{ 
    <table border="1" id="drawDocument"> 
     <colgroup> 
      <col span="1" style="width: 10%;" /> 
      <col span="1" style="width: 40%;" /> 
      <col span="1" style="width: 25%;" /> 
      <col span="1" style="width: 25%;" /> 
     </colgroup> 
     @Html.Partial("_PartialHeader", Model) 
     @Html.Partial("_PartialDrawing", Model) 
     @Html.Partial("_PartialBody", Model) 
     @Html.Partial("_PartialFooter", Model) 

    </table> 
    if (ViewBag.Status == 1) 
    { 
     <button type="submit" id="submitDocument">Запази</button> 
     <button style="float:right;" id="finalizeDocument">Приключи</button> 
    } 
    else 
    { 
     @Html.ActionLink("Назад", "Index") 
    } 
} 
</div> 
<div id="imageContainer"> 
<div id="imageGallery" style="overflow: scroll"> 
<img src="file:\\..." alt="docImg" style="width: 190px; height: auto"/> 
@Ajax.ActionLink("Delete", "DeletePicture", new { documentID = Model[0].Id }, 
         new AjaxOptions 
         { 
          Confirm = "Are you sure?", 
          OnComplete = "$('#blah').attr('src', '#').attr('style', 'display:none;'); $('#Image1').attr('src', '#').attr('style', 'display:none;'); $('#DelPic').attr('style', 'display:none;');" 
         }) 
<img src="file:\\..." alt="docImg" style="width: 190px; height: auto"/> 
    @Ajax.ActionLink("Delete", "DeletePicture", new { documentID = Model[0].Id }, 
         new AjaxOptions 
         { 
          Confirm = "Are you sure?", 
          OnComplete = "$('#blah').attr('src', '#').attr('style', 'display:none;'); $('#Image1').attr('src', '#').attr('style', 'display:none;'); $('#DelPic').attr('style', 'display:none;');" 
         }) 
</div> 
@using (Html.BeginForm("Upload", "Forms", FormMethod.Post)) 
{ 

    <input [email protected][0].DocumentId type="hidden" /> 

    <input type="file" name="datafile" id="file" onchange="readURL(this);" /> 
    <input type="button" name="Button" value="Upload" id="UploadButton" onclick="fileUpload(this.form,'/forms/upload','upload'); return false;"/> 
    <div id="upload" style="display: inline-block;"> 
     <img id="blah" src="#" alt="your image" style="display:none;"/> 
    </div> 
} 
</div> 
</div> 

第一种形式是其中I示出了用于当前表单/文档,并且因为数据他们是可编辑的我有提交按钮。我需要第二种形式将选定的图片提交给我的控制器,并在那里执行业务逻辑。

因此,一旦照片被选中,Upload按钮被点击我让我的控制器:

public ActionResult Upload(FormCollection collection) 
     { 
      WebImage UploadImage = WebImage.GetImageFromRequest(); 
      long documentID; 
      string finalImageName = null; 
      if (!long.TryParse(collection.AllKeys[0], out documentID)) 
      //More code... 

在那里我的形象和它属于和我需要的是执行文件的ID一些检查/验证,最后将选定的图像隐藏到专用目录并将名称保存到数据库中。

的问题是,我已经写除了一个将显示像不同的输出正确的消息的所有逻辑:

if (imagePath.Length > 247) 
          { 
           //TODO message that the path is too long 
           //TODO this return View() is temp, replace with something suitable 
           return View(); 
          } 
        //... 
         System.IO.File.Copy(UploadImage.FileName, imagePath); 
         } 
         catch (Exception ex) 
         { 
          //TODO copy failed return message 
          return View(); 
         } 
        //... 

这些是在相同的方法和执行中的所有不同的输出我想为每一个人展示一个正确的信息。我不确定的是,如果我仍然可以选择保存我的工作并仍然执行消息逻辑?现在看来,如果我以某种形式使用Ajax,现在看起来会容易很多,但我不是。我能想到的唯一想法就是创建ViewBag属性,并将其与模型一起返回到视图中,以检查不同的属性,并根据需要显示一些消息,但这意味着在我的视图中有很多额外的逻辑,重新发送我已经在我的视图中显示的数据库中的数据,简而言之,我认为这是一种糟糕的编程,但也许我已经将自己纳入了这一工作。那么从这里开始最好的行动是什么?是否最好只删除我的代码,并寻找一种方法来与AJAX做到这一点?

回答

2

您无法使用AJAX上传文件 - 您需要某种第三方解决方法。您需要使用Upload()方法返回原始视图,并使用适当的模型,并在ViewBag中的某个位置显示一个标记以显示消息,例如,

public ActionResult Upload(UpdateDocumentModel model) { 
... 
    if (imagePath.Length > 247) { 
    model.ErrorMessage = Errors.Over247; 
    return View("UpdateDocument", model); 
    } 
... 
return RedirectToAction("UploadOk"); 
} 

我已经改变了你的FormCollection,为便于阅读一个强类型的模型,再加上这是MVC.net是有什么。 Errors.Over247可能是项目中某处的字符串资源,或者是视图然后读取以显示某段HTML的布尔标志。

+0

嗯,说实话我真的不喜欢的'ViewBag'选项,但如果它是最好的...... – Leron

+0

你并不需要一个ViewBag,这只是为了说明,你可以将“Message”属性添加到您应该使用的UpdateDocumentModel上。 – CodingIntrigue

0

只是用它代替ViewBag TempData的

TempData["ErorrMessegge"] = "SomeMessage to view"; 


@TempData["ErorrMessegge"] 
+0

嗯,我还没有看到'TempData'之前是关键字在asp.net mvc? – Leron

+0

它是asbstract类ControllerBase的一个TempDataDictionary属性。本字典中的数据仅在当前请求中“存在”。 – maxs87

+0

并通过RedirectToAction工作 – maxs87

0

您可以简单地使用TempData的[],以便从控制器传递参数查看如下图所示:

控制器:

[HttpPost] 
public ActionResult Add(Applicant applicant) 
{ 
    repository.SaveApplicant(applicant); 
    TempData["message"] = "The applicant has been saved succesfully."; 
    return View(applicant); 
} 


查看:

@if (TempData["message"] != null) 
{ 
    <div>@TempData["message"]</div> 
} 
相关问题