【问题标题】:inline models editor MVC3. Again内联模型编辑器 MVC3。再次
【发布时间】:2012-10-18 00:15:10
【问题描述】:

朋友.. 我还有一个麻烦。 可能很愚蠢,但我看不出有什么问题。

    public ActionResult Images(Guid? id)
    {
        ViewBag.Gallery = _core.GetGalleryByID(_client, (Guid)id);

        List<ImageModel> models = new List<ImageModel>();

        foreach (var img in _core.GetImagesByGalleryID(_client, (Guid)id))
        {
            ImageModel model = new ImageModel(_client);
            model.Thumbneil = img.Thumbneil;
            model.Description = img.Description;
            model.AlternateText = img.AlternateText;
            model.GalleryID = img.GalleryID;
            model.ID = img.ID;
            models.Add(model);
        }

        ViewBag.Images = models;
        return View();
    }

    [HttpPost]
    public ActionResult SaveImageInfo(ImageModel imageModel)
    {
        Image img = _core.GetImageByID(_client, imageModel.ID);
        img.AlternateText = imageModel.AlternateText;
        img.Description = imageModel.Description;

        _core.SaveImageInfo(_client, img);
        return View();
    }

这是控制器的一部分。 这是部分观点:

@model WebUI.Models.ImageModel
@{
    ViewBag.Title = "Images";
    Layout = "~/Views/Admin/_Layout.cshtml";
}

<h2>@ViewBag.Gallery.Name</h2>

<table id="grid-table" >

    @foreach (var image in ViewBag.Images)
    {
    <tr>
        ....
        <td >
            @using(Html.BeginForm("SaveImageInfo", "Admin", FormMethod.Post))
            {
                @Html.HiddenFor(m => m.ID)
                @Html.TextAreaFor(m => m.Description) <br />
                @Html.TextBoxFor(m => m.AlternateText) <br />

                <div id="item-post" >
                    <input title="Подтвердить" type="submit" value="Подтвердить" />
                </div>
            }
        </td>

    </tr>
    }

</table>

和型号:

    public class ImageModel
{
    public Byte[] Thumbneil { get; set; }

    [Required]
    public String Description { get; set; }

    [Required]
    public Guid GalleryID { get; set; }

    [Required]
    public String AlternateText { get; set; }

    [Required]
    public Guid ID { get; set; }
}

在 ActionResult SaveImageInfo(ImageModel imageModel) 我应该有 2 个 Guid:ID 和 GalleryID。但。 我有这个: 这是我的问题:为什么?我看不到...

【问题讨论】:

    标签: c# .net asp.net-mvc-3


    【解决方案1】:

    在我看来,您需要在视图中为 GalleryId 添加一个 HiddenFor:

    @Html.HiddenFor(m => m.GalleryId)

    【讨论】:

    • 隐藏字段 GalleryID 是否呈现在 HTML 中?
    • 当然也应该是@Html.HiddenFor(m => m.GalleryID)
    • 不,它没有被渲染。 "
    • 好的,所以隐藏字段 GalleryId 的值为空,所以问题一定出在源数据上......即模型.GalleryID = img.GalleryID;不工作。另外,我建议使用 Automapper 将源对象映射到视图模型……比手动输入映射要容易得多。
    • 我认为,@Html.HiddenFor(m => m.ID) 及周围存在问题,因为听到:@foreach(ViewBag.Images 中的 var image)我看到所有 ID...跨度>
    猜你喜欢
    • 2010-10-16
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多