tags:

views:

160

answers:

4

I'm wanting to update a record in my database which has two values, one is the ID, and one is the "description". The ID can never be changed, however I'm relying on the use of strongly-typed features to do it. So, I have the following:

Inherits="System.Web.Mvc.ViewPage<Business>"

Which is fine as it allows me to get everything back. The problem is when I use the following line:

<%= Html.TextBox("BusinessID", ViewData.Model.BusinessID, new { disabled = "disabled", style = "width:50px;", @class = "uppercase", maxlength = "4" })%>

With the disabled = "disabled" option it doesn't recognise the BusinessID and therefore doesn't pass it back to the controller which, in turn has problems binding the object up.

Not that you'll need it, but here's the controller action:

[HttpPost]
public ActionResult EditBusiness(Business business)
{
  if (!ModelState.IsValid)
    return View(business);
  // update business here
  _contractsControlRepository.UpdateBusiness(business);
  return RedirectToAction("Businesses");
}

Any ideas why this is happening? I didn't realise form elements were completely hidden on postback when they're disabled. I don't want the users editing that particular field. I've also tried Html.DisplayFor(b=>b.BusinessID) without any luck.

A: 

instead of Html.Textbox you can use Html.Hidden("BusinessID", ViewData.Model.BusinessID)

Omu
See my comment to griegs.
Kezzer
A: 

Is there a particular reason you are displaying the id? if not then leave it out and on your controller simply use TryUpdateModel() instead.

or is that not what your asking?

edit

<%= Html.TextBox("name","value", null, new { style="readonly"}) %>

Edit 2

You might think about doing a route like //site/controller/yourbusinessid

then you can use the id as it's passed to your controller and you can then, in your view, simply use <%= Model.BusinessId %> as a string.

griegs
I do need to display the ID yes. The ID isn't an integer value, it's a manually entered varchar field which the user needs to see on screen whilst editing.
Kezzer
in that case can you use in your textbox new { style="readonly"}?
griegs
+1  A: 

display the id like this

Html.Hidden("BusinessID", ViewData.Model.BusinessID)
<%=Model.BussinessID %>

this way you will have the id for the binding in the hidden tag and you will display the value in the label or you can use anything else that you want yo can do like this also

 <input type="text" value="<%=Model.BussinessID %>" contentEditable="false">

and put the hidden somewhere in the form

Omu
Bingo, thanks very much.
Kezzer
Also, from what you've given me I can just create a helper to do it for me.
Kezzer
A: 

You always have the option of either "hard-coding" the html element, or writing your own html helper method to do it.

public static string DisabledTextBox(this HtmlHelper helper, string name, object value)
{
    return String.Format(@"<input type="text" name="{0}" id="{0}" disabled="disabled" value="{1}" />", name, value);
}
Tomas Lycken