MVC 2 model properties not used in view returned as null or empty
Date : March 29 2020, 07:55 AM
will be helpful for those in need This is the way MVC works - it tries to construct an object of the action parameter type from the values in the route, form and query string. In your case it can only get the values in the form collection. It does not know anything about the values that you have stored in your database. If you are only interested in certain properties you would be better off doing a specific view model with just these on - then you can validate for this specific case.
|
Partial view returns the Model with empty fields
Date : March 29 2020, 07:55 AM
This might help you I was able to solve the issue simply by adding an hidden field for each needed property, like: @Html.HiddenFor(p => p.Entity.OrderId, new { id = "OrderId" })
|
MVVM : how to make view model set fields of clean model to persist view changes to database
Date : March 29 2020, 07:55 AM
hop of those help? If GetSubsystem returns a new subsystem every time, that's your problem. In the 'set' for the properties you're binding to the view, you're calling the public property "Subsystem", not the private field you've created. So, every single time you set a property from the view, you are calling Subsystem.get which calls GetSubsystem(SelSubsystem.SubsystemNo);. I think, in your ViewModel properties', you want to change it to: //Properties to which View is bound
public int? Serial
{
get { return _subsystem.Serial; }
set
{
_subsystem.Serial=value; // NOTE THE USE OF THE PRIVATE FIELD RATHER THAN THE PROPERTY
OnPropertyChanged("Serial");
}
}
public string Type
{
get { return _subsystem.Type; }
set
{
_subsystem.Type = value; // NOTE THE USE OF THE PRIVATE FIELD RATHER THAN THE PROPERTY
OnPropertyChanged("Type");
}
|
Generate labels, textboxes, validation messages for all properties in view model
Tag : chash , By : Goeran
Date : March 29 2020, 07:55 AM
like below fixes the issue You could simplify it by using UIHint on your model fields like this: Model: public class UserModel
{
[UIHint("UserModelField")]
public string Username { get; set; }
[UIHint("UserModelField")]
public string Password { get; set; }
}
@model string
@Html.LabelFor(m => m)
@Html.TextBoxFor(m => m)
@Html.ValidationMessageFor(m => m)
@model UserModel
@Html.EditorFor(m => m.Username)
@Html.EditorFor(m => m.Password)
public ActionResult ViewUser()
{
var model = new UserModel { Username = "a", Password = "p" };
return View(model);
}
|
Empty fields when loading a view from a controller's action returning a View Model
Tag : chash , By : judith
Date : March 29 2020, 07:55 AM
Hope that helps I'm trying to edit a record, but when the view is rendered, the data is not showing (all the fields are empty). I debugged the view model and I can see the data is being loaded correctly. , I found the error, i have to include all navigations public ActionResult Edit(Guid? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Cliente cliente = db.Clientes.Include(x => x.PessoaFisica).Include(x => x.PessoaFisica.Pessoa).Include(x=> x.PessoaFisica.Pessoa.Endereco).FirstOrDefault(x=>x.Id == id);
if (cliente == null)
{
return HttpNotFound();
}
return View(cliente);
}
|