1
votes

I have a grid when the client click on Edit for one like a form is open. the User can Edit only some values (let the user to edit only the Shipping date for the current order) but when I send the Form the values of the non editable field are NULL on Post

When I display :

@Html.Display(model => model.Rep)

or :

@Html.TextBoxFor(model => model.ClientName, new { disabled = "disabled", @readonly = "readonly" }) 

the Values are displayed correctly but when I submit the form the Value are Null. the View :

@model Models.Orders
@{
    Layout = null;
}
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link href="~/Content/pure-release-0.5.0/pure-min.css" rel="stylesheet" />
</head>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <body>
    <form class="pure-form">
        <fieldset>
             <legend>A Stacked Form</legend>
            <div class="pure-g">
                <div class="pure-u-1 pure-u-md-1-3" aria-disabled="true" aria-readonly="true">
                    <label for="first-name">@Html.LabelFor(model => model.Rep)</label>
                    @Html.Display(model => model.Rep)
                </div>
                <div class="pure-g">
                    <div class="pure-u-1 pure-u-md-1-3" aria-readonly="true">
                        <label for="first-name">@Html.LabelFor(model => model.ClientName)</label>
                        @Html.TextBoxFor(model => model.ClientName, new { disabled = "disabled", @readonly = "readonly" }) 
                    </div>
                </div>
            </div>
            <p>
                <input type="submit" value="Save" />
            </p>
        </fieldset>
    </form>
    </body>
}
</html>
<script type="text/javascript">
    $(document).ready(function () {
        $(".Titre").click(function () {
            $(this).next('.Contenu').slideToggle("slow");
        });
        $("#Model").prop('disabled', true);
    });
</script>


@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}^

The model :

 public class Orders
{
        public int ID { get; set; }
        public string Rep { get; set; }
        public string ClientName { get; set; }
}

Controller :

When the User click on Edit on the Grid:

 public ActionResult Edit(int id = 0)
        {
            Orders order = db.Orders.Find(id);
            if (order == null)
            {
                return HttpNotFound();
            }
            return View(order);
        }

On post:

[HttpPost]
public ActionResult Edit(Orders order)
    {
        if (ModelState.IsValid)
        {
            db.Entry(order).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(order);
    }

When I debug I find theat the Values on order are NULL

I Thought that the problem was the waay that I sent data from my grid to the form but i change the easyui to Use GridMVC and still have the problem.

I used : in View TextBoxFor for readOnly + disabled as attribut but same problem

I tried : in the Model : [ReadOnly(true)] + in the View : @Html.EditorFor(model => model.Rep) but I was able to edit Rep That I want to block

I tried to make the EditorFor readonly with Javascript but I was able to edit

Can you help me please, I tried all what I found but there is something missing in my code

Thanks

4

4 Answers

1
votes

This is by design. Readonly values are not submitted to server (at least by modern browsers).

If you want to submit this value you can create a hidden field instead of a textbox:

@Html.HiddenFor(model => model.ClientName)

This will effectively submit your value to server

0
votes

I ran into the same issue. Trying to short cut with the MVC CRUD template. I like what @PaulTaylor suggested:

Attribute [Bind(Exclude=“”)] fails to prevent over-posting

0
votes

The problem is because you are using 2 forms: one nested to the other. You have to remove the second and add the css class to the first. The Html.BeginForm() will create another form. You can try something like this:

 @using (Html.BeginForm("Edit","controllerName",null,FormMethod.Post,{@class="pure-form"}))
{
 @Html.ValidationSummary(true)
 <body>
   @Html.HiddenFor(model => model.ID)    
    <fieldset>
         <legend>A Stacked Form</legend>
        <div class="pure-g">
            <div class="pure-u-1 pure-u-md-1-3" aria-disabled="true" aria-readonly="true">
                <label for="first-name">@Html.LabelFor(model => model.Rep)</label>
                @Html.Display(model => model.Rep)
            </div>
            <div class="pure-g">
                <div class="pure-u-1 pure-u-md-1-3" aria-readonly="true">
                    <label for="first-name">@Html.LabelFor(model => model.ClientName)</label>
                    @Html.TextBoxFor(model => model.ClientName, new { disabled = "disabled", @readonly = "readonly" }) 
                </div>
            </div>
        </div>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
</body>
}
0
votes

Your text box is disabled. The inputs with disabled attribute set do not get submitted with the form. You can verify this by using network tab of Chrome dev tool or your favorite browser. If you want the text boxes disabled, use just readonly and not the disabled. So, your text box should be as below:

 @Html.TextBoxFor(model => model.ClientName, new {@readonly = "readonly" }) 

Afterwards, you can use jQuery and CSS to make them look like disabled by graying them out if you like but do not use disabled if you want the values to come through.