简体   繁体   English

将表单发布到控制器时,缺少ID

[英]The id is missed when form is posted to controller

I am developing a mvc website.I have a table called member .this table has a controller and the controller has an edit method as you can see : 我正在开发一个mvc网站。我有一个名为member表。此表有一个控制器,该控制器有一个edit方法,如您所见:

 public ActionResult Edit()
        {
            int userId = _memberRepository.ReturnMemberIdByMobile(User.Identity.Name);
            ViewBag.Edit = _memberRepository.FindById(userId).First();
            return View();
        }
        [HttpPost]
        public ActionResult Edit(Member value)
        {
            try
            {
                if (_memberRepository.Edit(value))
                {

                    value.RegisteredDate = DateTime.Now;
                    _memberRepository.Save();
                    TempData["Success"] = "با موفقیت ویرایش شد ...";
                    string strLocation = HttpContext.Server.MapPath("~/Image/users");
                    if (value.ImgByte != null)
                    {
                        value.ImgByte.SaveAs(strLocation + @"\" + value.Id + ".jpg");
                    }
                }

            }
            catch (Exception)
            {
                TempData["Error"] = "ویرایش نشد، لطفاً مجدداً تلاش نمایید";


            }
            return RedirectToAction("Edit");

        }

The edit view is correctly work.the problem is when i post my view to edit controller .the id of member is changed to 0 it means it is missed.why ?and the value can't be edited. 编辑视图正确工作。问题是当我将视图发布到编辑控制器时。成员的id更改为0这意味着它丢失了。为什么?并且该值无法编辑。

  @using DCL
@{
    ViewBag.Title = "Edit";
    Layout = "~/Areas/user/Views/Shared/_shared.cshtml";
    Member membervalue = new Member();
    membervalue = ViewBag.Edit;

}

@using (@Html.BeginForm("Edit", "User", FormMethod.Post,
    new {id = "form", enctype = "multipart/form-data"}))
{
    if (TempData["Error"] != null)
    {
        <div class="pad margin no-print">
            <div class="callout callout-info" style="margin-bottom: 0 !important; background-color: #ea0000 !important; border-color: #d20000">
                @TempData["Error"]
            </div>
        </div>
    }
    if (TempData["Information"] != null)
    {
        <div class="pad margin no-print">
            <div class="callout callout-info" style="margin-bottom: 0 !important; background-color: orangered !important; border-color: red">
                @TempData["Information"]
            </div>
        </div>
    }
    if (TempData["Success"] != null)
    {
        <div class="pad margin no-print">
            <div class="callout callout-info" style="margin-bottom: 0 !important; background-color: #00A65A !important; border-color: #00925A">
                @TempData["Success"]
            </div>
        </div>
    }
    <div class="row">
        <!-- general form elements -->
        <div class="col-xs-12">
            <div class="box">
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title">حساب کاربری</h3>
                    </div>
                    <!-- /.box-header -->
                    <!-- form start -->
                    <div class="box-body">
                        <div class="col-lg-7">
                            <div class="input-group">
                                <label for="Name">نام</label>
                                <input class="form-control" id="Name" name="Name" type="text" value="@membervalue.Name">
                            </div>
                            <div class="input-group">
                                <label for="family">نام خانوادگی</label>
                                <input class="form-control" id="family" name="family" type="text" value="@membervalue.Family">
                            </div>
                            <div class="input-group">
                                <label for="mobile">موبایل</label>
                                <input class="form-control" id="mobile" name="mobile" type="text" value="@membervalue.Mobile">
                            </div>
                            <div class="input-group">
                                <label for="password">رمز عبور</label>
                                <input class="form-control" id="password" name="password" type="password" value="@membervalue.Password">
                            </div>
                            <div class="input-group">
                                <label for="Email">ایمیل</label>
                                <input class="form-control" id="Email" name="Email" type="text" value="@membervalue.Email">
                            </div>
                            <div class="form-group">
                                <label for="ImgByte">عکس </label>
                                <input id="ImgByte" name="ImgByte" type="file">
                            </div>
                                     <input type="hidden"  id="Id" name="id" value="@membervalue.Id">

                        </div>
                    </div>
                    <!-- /.box-body -->
                </div>
            </div>
        </div>
        <!-- /.box -->

    </div>
        <div class="row" style="margin: 0; margin-bottom: 20px">
        <div class="box-footer" style="direction: ltr">
            <button type="submit" class="btn btn-info">ویرایش</button>
            <a class="btn btn-gray" href="@Url.Action("Index", "Home", null)">انصراف</a>
        </div>
    </div>
}

Instead of using the viewbag for your model you should pass in the model as a strongly typed object. 不应在模型中使用视图包,而应将模型作为强类型对象传递。 You can do this with the following change in the Action. 您可以通过在Action中进行以下更改来做到这一点。 Then in your view define the model at the top and you can use it throughout the code. 然后,在视图中的顶部定义模型,然后就可以在整个代码中使用它。 You will also need a @Html.HiddenFor tag for your id. 您还需要一个@ Html.HiddenFor标签作为您的ID。 Now it is no longer possible (without a compile time exception that is) to create a type-o. 现在,不再有可能(没有编译时例外)创建type-o。 On your previous code maybe you cased Id incorrectly which would cause it not to be populated OR maybe the form field name was not cased correctly. 在您以前的代码中,您可能错误地设置了Id的ID,这将导致它不被填充,或者表单字段名没有正确地被识别。 This takes all those manual errors out of the equation. 这消除了所有这些手动错误。

    public ActionResult Edit()
    {
        int userId = _memberRepository.ReturnMemberIdByMobile(User.Identity.Name);
        var model = _memberRepository.FindById(userId).First();
        return View(model); // pass this in as the model, do not use viewbag
    }

View 视图

@model = Member @* namespace qualified type *@

@*... editor code *@
@Html.HiddenFor(x => x.Id)
@Html.TextboxFor(x => x.Name) @* do this instead of manual input *@

I ran your code, and there is no mistake in it. 我运行了您的代码,其中没有错误。 Id is passed correctly to Edit (post) action. ID已正确传递到“编辑(发布)”操作。 The only reason it can be empty in the code that you show is that FindById returned entity without Id property set. 它在您显示的代码中可以为空的唯一原因是FindById返回的实体没有设置Id属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM