I have the following code:
#foreach (var item in #ViewBag.list)
{
<span>#item.Id </span>
}
The ViewBag gets the data from a service that contains a class that gets the data from a database, but I'm getting a null exception error since the ViewBag is null because a user has to first enter a date to be able to get the list of ids.
public async Task<ActionResult> plan_post(DateTime dateselected)
{
DateTime date = dateselected;
var plan = await _ps.PlanbyDate(date, date);
ViewBag.list = plan;
return View();
}
I'm a student doing an internship can you help me with this?
this is the error:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Fo fixed your problem you must debugging your plan object may be null !!!
But you must check if viewbag null not render foreach
if(ViewBag.list != null)
{
//write for each in here
}
You have more options for pass Model to view.
I recommend you use pass with View function
retune View(plan)
But must declaration datatyoe model in top page view
#model YourDataType
Related
In the Create action of the controller, based on user input, we plan to populate the model object with some data, to minimize data entry:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(Item item, string str)
{
// if only str is provided
if (string.IsNullOrEmpty(item.KeyInfo) && !string.IsNullOrEmpty(str))
{
Helpers.FillItemModel(item, str); //fill data
}
else if (ModelState.IsValid)
{
_context.Add(item);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Create));
}
return View(item);
}
However, although we can confirm the item object has been populated with data to several fields, by setting break point at the last line: return View(item), when the browser gets the response, all fields are empty.
But if we comment out the entire code segment, only leave the return statement and do a post with some data that was manually entered, the browser will receive correct data in all fields.
Thank you for your time.
To update ModelState value you have to reset the ModelState first as follows:
// if only str is provided
if (string.IsNullOrEmpty(item.KeyInfo) && !string.IsNullOrEmpty(str))
{
ModelState.Clear();
Helpers.FillItemModel(item, str); //fill data
}
ModelState.Clear() will reset the whole model. If you don't want that and just want to update few fields value keeping other field value intact then use ModelState["fieldName"].Value = "newValue in your helper class.
I am creating an CRUD Application in Asp.Net Core
After Add Operation I am redirecting to same view with setting model value as null to get another entry
Below is my code
public IActionResult Add(OptionMasterVM model)
{
try
{
model.QuestionList = context.QuestionMaster.Select(x => new SelectListItem { Text = x.QuestionName, Value = x.QuestionId.ToString() }).ToList();
if (HttpContext.Request.Method == "POST")
{
OptionMaster _optionmaster = new OptionMaster();
_optionmaster = model.OptionMaster;
using (var ctx = new QuestionnaireEntities(_configuration))
{
ctx.OptionMaster.Add(_optionmaster);
ctx.SaveChanges();
}
TempData["Msg"] = "Option Added Successfully , Add Another Option";
model.OptionMaster.OptionValue = string.Empty;
model.OptionMaster.OptionRating = 0;
return View(model);
}
}
catch (Exception ex)
{
logger.LogError(ex);
}
finally
{
}
return View(model);
}
Here I am setting Option Value to empty and rating to Zero to take next entry , but on view it does not show empty and zero , on view it show previously filled value.
After Setting below code these two fields should be reset but they don't
model.OptionMaster.OptionValue = string.Empty;
model.OptionMaster.OptionRating = 0;
Is there any other way to set model object as null in Asp.net Core ?
This can happen because Razor helpers use values from ModelState, rather than the model itself. Your OptionValue is probably displayed using a helper, for example:
#Html.TextBoxFor(m => m.OptionMaster.OptionValue)
When you change model values within an action, you need remove the old values from ModelState before rendering the View.
The easiest way of doing this is to call ModelState.Clear()
model.OptionMaster.OptionValue = string.Empty;
model.OptionMaster.OptionRating = 0;
ModelState.Clear(); // ensure these changes are rendered in the View
return View(model);
The values displayed for bound form fields come from ModelState, which is composed based on values from Request, ViewData/ViewBag, and finally Model. After posting, obviously, you'll have values set in Request, which will therefore be the values in ModelState. It works this way, so that when there's a validation error and the user is returned to the form to correct their mistakes, the values they posted will be there for them to edit.
Long and short, you need to follow the PRG (Post-Redirect-Get) pattern. Essentially, after posting, you only return the view on error. If the post is successful, you redirect. This not only clears ModelState, but also prevents accidental re-posts if the user attempts to refresh the page.
If you want to take the user back to the same view, simply redirect to the same action, but you need to do a redirect, not return the view.
I'm new to using MVC 5 and .NET framework 4.5 and I have encountered an issue. I have a form, and I want to reset the fields of the form when it errors out, but keep the errors and display to the user. Currently I can clear the fields on error, but this also gets rid of the errors. I have tried
originalModel.field= "";
but this doesn't clear the field. I have also tried just using
ModelState.Clear();
But this doesn't do anything either.
This is what I'm currently working with (which clears everything):
if (!ModelState.IsValid)
{
TestModel blank= new TestModel();
ModelState.Clear();
return View("View.cshtml", blank);
}
If you absolutely need this behavior, you can go and set the value of this property of your view model to empty string in the Modelstate dictionary.
[HttpPost]
public virtual ActionResult Index(CreatePostVm model)
{
if (ModelState.IsValid == false)
{
ModelState["PostTitle"].Value =
new ValueProviderResult(string.Empty, string.Empty,CultureInfo.InvariantCulture);
return View(model);
}
// to do : Return something
}
The above code will set the PostTitle property of your CreatePostVm view model to an empty text. So in your view, the input field will be an empty text while still displaying the validation error message for PostTitle field.
As stated in the title: how does razor know when the page should be rendered in error or normal state?
I need to know this information so I can display a page in error mode with validation messages displayed etc. Unfortunately I cannot simply do View() because the code which finds the model in invalid state is in subview.
EDIT
I'm trying to do something like this to force razor to render a view in error mode:
// just for tests....
// model: a model which was marked as invalid in different controller
// state: state of the model from that controller
public ActionResult asdf(TModel model, ModelStateDictionary state) {
var result = View(this.Partial, model);
result.ViewData.ModelState.Clear();
foreach (var x in state) {
result.ViewData.ModelState.Add(x.Key, x.Value);
}
return result;
}
EDIT2
Final solution. In the previous attempt auto-deserialization from Json to c# types didn't work (sic!) so I've decided to receive a plain json string and deserialize it with another library like so:
public ActionResult ErrorIndex(string jsonParamsString) {
var param = Newtonsoft.Json.JsonConvert.DeserializeObject<ForceInvalidStateRequestArg<TModel>>(jsonParamsString);
if (param != null && param.Errors != null) {
this.ModelState.Clear();
foreach (var s in param.Errors) {
this.ModelState.AddModelError(s.PropertyName, s.ErrorMessage);
}
}
var result = View(this.PartialName, param == null ? this.NewModel : param.Model);
return result;
}
Information about model validity is stored in ModelState object.
Which is accessible in controller like:
this.ModelState.IsValid
In view:
this.ViewContext.ViewData.ModelState
To your EDIT:
You can add model validation errors like this:
this.ModelState.AddModelError("key", "an error message");
So if you want to force #razor to render validation error messages. It could look like:
public ActionResult asdf(TModel model, ModelStateDictionary state)
{
var result = View(this.Partial, model);
result.ViewData.ModelState.Clear();
foreach (var pair in state.Where(m=> m.Value != null && m.Value.Errors.Any()))
{
result.ViewData.ModelState.AddModelError(pair.Key, string.Join(",",pair.Value.Errors.Select(e=>e.ErrorMessage).ToArray()));
}
return result;
}
I have been introduced to Razor as applied with MVC 3 this morning, so please forgive me if my question seems terribly uninformed!
I am working with an app whose workflow involves allowing a user to select a value (warehouse) from a drop down list, and add a record (material) from that warehouse to another record (Materials Request). Once the first material has been added to the Materials Request, I need to permanently set the value of the drop down to the warehouse that was first selected, then disable the drop down control (or set to read only, perhaps). The existing code in the razor file uses the DropDownListFor() method, including a ViewBag collection of Warehouse records. I have seen discussions which suggest abandoning the ViewBag design, but honestly I don't have the desire to rewrite major portions of the code; at least it looks like a major rewrite from the perspective of my experience level. Here's the original code:
#Html.LabelPlusFor(m => m.WarehouseId, "*:")
#Html.DropDownListFor(m => m.WarehouseId, (IEnumerable<SelectListItem>)ViewBag.WarehouseCodes, "")<br />
I believe I have been able to select a value based on a session object, though I'm still not sure how to disable the control. Here's my change:
#{
int SelectedWarehouseId = -1;
if (HttpContext.Current.Session["SelectedWarehouseId"] != null)
{
SelectedWarehouseId = Int32.Parse(HttpContext.Current.Session["SelectedWarehouseId"].ToString());
}
}
#Html.LabelPlusFor(m => m.WarehouseId, "*:")
#{
if (SelectedWarehouseId > -1)
{
#Html.DropDownListFor(m => m.WarehouseId, new SelectList((IEnumerable<SelectListItem>)ViewBag.WarehouseCodes, "WarehouseId", "WarehouseDescription", (int)SelectedWarehouseId))<br />
}
else
{
#Html.DropDownListFor(m => m.WarehouseId, (IEnumerable<SelectListItem>)ViewBag.WarehouseCodes, "")<br />
}
}
When the material is added to the Material Request, the WarehouseId is passed to the controller and I can access that value as "model.WarehouseId" in the controller class. However, I'm not sure how to get that value back to the View (apologies for the large code block here):
[HttpPost]
[TmsAuthorize]
public ActionResult Create(ItemRequestViewModel model)
{
string deleteKey = null;
//Removed code
else if (Request.Form["AddToRequest"] != null)
{
// If the user clicked the Add to Request button, we are only
// interested in validating the following fields. Therefore,
// we remove the other fields from the ModelState.
string[] keys = ModelState.Keys.ToArray();
foreach (string key in keys)
{
if (!_addToRequestFields.Contains(key))
ModelState.Remove(key);
}
// Validate the Item Number against the database - no sense
// doing this if the ModelState is already invalid.
if (ModelState.IsValid)
{
_codes.ValidateMaterial("ItemNumber", model.ItemNumber, model.WarehouseId);
Session["SelectedWarehouseId"] = model.WarehouseId;
}
if (ModelState.IsValid)
{
// Add the new Item Request to the list
model.Items.Add(new ItemViewModel() { ItemNumber = model.ItemNumber, Quantity = model.Quantity.Value, WarehouseId = model.WarehouseId });
ModelState.Clear();
model.ItemNumber = null;
model.Quantity = null;
model.WarehouseId = null;
}
}
//Removed code
return CreateInternal(model);
}
private ActionResult CreateInternal(ItemRequestViewModel model)
{
if (model != null)
{
if (!String.IsNullOrEmpty(model.SiteId))
{
ViewBag.BuildingCodes = _codes.GetBuildingCodes(model.SiteId, false);
if (!String.IsNullOrEmpty(model.BuildingId))
ViewBag.LocationCodes = _codes.GetLocationCodes(model.SiteId, model.BuildingId, false);
}
//Removed code
}
//Removed code
ViewBag.WarehouseCodes = _codes.GetWarehouseCodes(false);
return View("Create", model);
}
So my questions are, how do I disable the drop down list, and how can I pass a value for the selected WarehouseId back to the view? I've also considered adding the value to the ViewBag, but to be honest I don't know enough about the ViewBag to recognize any unintended consequences I may face by just randomly modifying it's contents.
Thanks for any help offered on this.
Without going into which approach is better...
Your dropdown should be rendered as an HTML select element, in order to disable this you'll need to add a disabled="disabled" attribute to it.
The DropDownListFor method has a htmlAttributes parameter, which you can use to achieve this:
new { disabled = "disabled" }
when your pass model to your view like
return View("Create", model);
if WareHouseID is set in model then
Html.DropDownListFor(x=>x.WareHouseID, ...)
will automatically set the selected value and u don't have to do that session processing for this. So far as disabling a field is required, stewart is right. you can disable drop down this way but then it won't be posted to the server when u submit the form. you can set it to readonly mode like
new{#readonly = "readOnly"}