table division text dissapears after validation is run on submit - html

In my razor page I've got several check boxes arranged in a table. I've got some other #Html.EditorFor elements that are required inputs. When I submit, and the validations are run, the page refreshes with the eoor messages and the text next to my check boxes in the table disappears. What's up with that?
My checkboxes are made with #Html.CheckBoxFor
I'm not using any special stylings or class attributes or anything right now.

You are correct. When the form is posted, you will lose all of that data provided you do not resend it back into your view. In your controller, you need to return the model along with the view. Without seeing your code, I can't give a specific answer but it should look something like this:
public ActionResult DoSomethingWithFormPostData(Model yourModel)
{
//Do whatever you need to do.
return PartialView("_yourView", model);
}
Alternatively, I like to have a method in my controllers to I use for the sole purpose of populating a page. If you have something like that, you can refer back to that sending the model as a routevalue in this way:
public ActionResult DoSomethingWithFormPostData(Model yourModel)
{
//Do whatever you need to do.
return RedirectToAction("_yourView", "YourController", model);
}

Related

Nested form - detect change - ANGULAR

I have a problem detecting change in a form.
What is my goal: disable "Save" button unless there was a change in the form.
But the problem is that the form is nested and I don't know how to pass information about changed values from child components to the parent one neatly (...or at all).
My html looks like this:
...
<form (ngSubmit)="handleSave()">
<child-component [stuff]="stuff"
[someBoolean]="false">
</child-component>
<app-button [disabled]="someBoolean">Save
</app-button>
</form>
...
and the child component has some inputs, toggles, etc. and such form can have multiple child components.
Now is there a way to not do all the stuff with form groups and registering every input by itself (since it seems a little tricky to do this with nested components) but have a function for the button [disabled]="canSave()"
and then method in the ts file something like:
public formChanged: boolean = false;
public canSave(): void {
if(this.form changed){
this.formChanged = true;
}
}
and basically ask for a change in the form like a whole? (in a general sense, like: was there ANY change in the component, regardless of the fact that it is a form, but coincidentally the html component is a form)
I have also seen something like attribute dirty but it was again tied to form controls or groups or whatever.
Thanks a lot!

How to prevent old data in Model from being passed into the input fields automatically?

Is it possible to prevent the data from being automatically passed to the input fields. For example - if I have an edit page where I want to change the data and I want the user to type the data again without giving him the pre-filled old data. I was looking over some documentation about the ModelState and the asp-for tag but I just couldn't find the answer.
I was looking for help and I found this post here. Well I want to do exactly the opposite.
asp-for tag helper functionality_
EDIT:
I tried to change the value attribute of the input field to empty string and it worked, however I still would like to know if there is an asp.net approach.
If you don't want to pass data from action to Edit view,you can return a view with an empty model like this:
public IActionResult Edit()
{
return View(new YourModel());
}
Or you can try to use id and name attribute to replace asp-for,so that the data will not be binded to inputs.Here is a demo.
Change
<input asp-for="TestProperty" />
to
<input id="TestProperty" name="TestProperty" />

How to pass html element into rowDragText callback

I am not even sure if what I am trying to do is possible. I want to present the "floating" DOM element created when dragging a row as something more than just text. Seems like when I try to use html code as the returned value it is rendered as text rather than html:
rowDragText: function(params) {
return `<div [innerHTML]=${params.rowNode.data.RULE_NAME}></div>`;
}
This is what happens:
There is public static GHOST_TEMPLATE defined in dragAndDropService.ts.
You can try modifiying that template and perhaps also inspect the createGhost function nearby.
rowDragText probably not used with this method...

Dynamically re-bind html.ValidationMessageFor html helper?

Some background information, I am using ASP.NET with the MVC framework and html helpers.
I currently have a dynamic table where each row has a series of input boxes. Each of these input boxes has a validation message. This works completely fine for the first row. However, when other rows are dynamically added (with the IDs' being changed along with other attributes to match the row number) the validation message no longer works.
Both the row and validation message span are being replicated properly.
In JQuery, this is usually just a problem with the binding, so for each row I would simply re-bind the IDs'. However I am not really to sure how to approach them in ASP.NET.
Any assistance would be appreciated.
Thanks
Alright, I have finally figured this out.
In MVC, in order to handle the validation, it import a JQuery file known as jquery.validate.unobtrusive.js.
However, similar to JQuery, this only occurs at the very beginning when the page is loaded. So, when you add a new dynamic element, you need to remove the bindings and the re-bind them again.
Basically, in your function for adding a new element, put the following lines of code AFTER you have added the new element:
$("#form").removeData("validator");
$("#form").removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse("#form");
For example:
function addInfoDynamic()
{
document.getElementById("#myDiv").innerHTML += "New Content";
$("#form").removeData("validator");
$("#form").removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse("#form");
}

ASP.Net MVC: How to dynamically generate a meta tag based on the content of the url?

Here is the idea:
When the user wants to see /controller/action, then I want the page to have a "robots" meta tag with its value set to "all".
When the user wants to see /controller/action?sort=hot&page=2 (i.e. it has a query string), then I want the page to have a "robots" meta tag with its value set to "noindex".
Of course this is just an example and it could be another tag for the existence of this question.
At what stage in the MVC architecture could I place a hook so that the view generates the tag I want in the master page?
I do something similar for generating page titles and it works well. Put your tag in your masterpage as normal:
<%= Html.Encode(ViewData["Title"]) %>
Then subclass ActionFilterAttribute and override OnActionExecuting. From there you get access to the controller context and can set your viewdata to whatever you want.
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Controller.ViewData["title"] = "whatever";
}
last step is to put Attribute your controllers that you want to use the filter context. You can inherit from a base controller if you want to add the attribute to all classes. There are also overloads if you want to pass parameters. In my app. I actually pass the page title.
Hope that helps.