Guidance on using Scoped Model Driven - struts2

My problem is I am retrieving the class but not able to set it and display it on the form while using the edit option.
The Flow is
CLICK on edit and retrieve the class to update
Now display the retrieved on the form and then update the value.
The problem is I am able to retrieve but not able to set the value in my form.
I am using model driven.
I searched on net and the solution is using scoped model driven but I am not getting any examples on how to use it.
Would be helpful if someone could guide me, I am stuck here.

Send the primary key and the operation=Edit on EditOption
-getModel will be called as soon as the action is called again
and in your action you can define as
if("Edit".equalsIgnoreCase(operation)){
user = userServiceInterface.get(user);----(get by primary key)
System.out.println or log->("user==> "+user);
}
now redirect it to your jsp with the form
Hope this helps

Related

How do I let the user update a record if the view doesn't contain the record ID?

I'm a beginner at web development, and I'm trying to make my first basic website with ASP.NET Core. I'm trying to implement a very basic thing, but I'm not sure how to do it.
I have two controller actions methods: LoadBlogPost and SaveBlogPost. LoadBlogPost returns a view that contains an existing blog post model, and SaveBlogPost updates it in the database.
This issue is that in order to update a record in the database, I need the record ID. But the model that SaveBlogPost receives does not contain the ID, because the ID is not part of the view. I've found that I can fix this issue by adding the ID to the view. However I don't want to do that.
How do web developers normally approach this? How do I make it so that the SaveBlogPost method will know the ID of the model it receives, without including it in the HTML view?
HTTP is a stateles protocol.
If you're trying to save an existing item, then your view model needs to return an ID which needs to be submitted on form post.
You DO send the ID , but you just don't show it .
In the .cshtml page for Update, you have to use input hidden
<form method="post" asp-action="Update">
<!--Id will be passed to action for updating-->
<input asp-for="Id" hidden />
Then you can use this Id in your actions.

asp.mvc how do I submit mulitple forms?

I have an asp.mvc app the presents to the user different forms when they click on the next button - like a wizard. I do it this way so I can use JQuery to validate each form as the user progresses through them. i.e.
...
// use jquery validator to funk up the form validation
// user clicks the Next button ...
switch(currPageIndex) {
case 0:
if($('#form1'.valid()) {
$('#form1').hide();
$('#form2').show();
}
break;
}
...
However I can't use a single submit button to post the all the forms formcollection data back to my controller - if I do I only get back the one form that the submit button was in and not all of them.
Is there some magic icantation I can type in to get all the forms data sent back to the controller?
Presumably I can cruft up the data myself and send it back to my controller as a jason string, but I'm not sure if this is the best way.
Many thanks.
Wizard-like forms basically rely on a model which is kept on the server-side (in session or a database table) and kept updated.
For example, each post from the client you get back the model from session or database and then call UpdateModel() using the FormCollection which updates the model and the you can check if it is valid.
You can only submit one form at a time, however there are multiple ways around this in your scenario. When you change to the second form you could populate some hidden fields using javascript that would contain the information from the previous form. Then it would all be in the second form and you wouldn't have a problem getting the information.
You could also do it via ajax/json, but then you would probably want to do it with both of the forms data anyway.
In the end I just json'd up the form data and ajax'd it over to the controller, it works very well, nice and and clean to.
many thanks for the replies.
You can do that using $('#form1').submit() function, I don't think there is another way.

.NET custom authorize attribute (mvc)

In certain Controller I have CRUD methods. In order to access these methods user needs to be logged in. This is why I used [Authorize] attribute for this controller. Now I need additional attribute which would check if item that user wants to view/delete/update belongs to him.
Is it possible and recommended to do this with attribute or you would suggest using check methods inside each method? If you suggest using attribute, could you please provide me some links/instructions?
EDIT:
Ofcourse, if attribute returns false than I don't want to redirect user to login page but show him an error message...
It can be done with a custom Authorize attribute, but it's much cleaner to put the logic inside your controller methods.
The attribute is related to the action being called (the controller class method). On that basis any attribute relating to the user's ownership of the object being manipulated (from your Model) should really be on the entity/class that the user is attempting to manipulate. You'll probably find it easier to validate the user within the Model method rather than using an attribute to achieve this.
In my opinion it is possible, just google for 'Custom Authorize Attribute'.
But maybe it is better to query your database with something like this:
ContextOrSession.Query<Something>.Where(Something.Groups.Intersect(User.Groups).Count>0)

Two-Way-Binding Possible In ASP.NET MVC?

Let's say I have a product object (pretty much empty) and I bind it to a Product view. Then I click update in the view. In my CustomModelBinder my bindingContext.Model is always null on the update request. Is there a recommended way of me retrieving the prior model at this point or do I always have to recreate it?
You have to recreate it from the form fields. The values you bound to the model for the GET are long gone.
perhaps im not understanding your needs to use a CustomModelBinder, but did u concider Data Annotations Model Binder yet?
it even comes with (serverside) validation based on simple statements like [Required] which u can put right inside your model, see this

asp.net mvc how to save?

I have the following model:
Customer:
ID
Name
Address
Phone
Fax
I added an Edit view based on the above model from the controller. I modified the Edit view to only allow edit on the Phone and Fax field (deleted the rest). When I submit it I get an error. It works if I leave the Edit view untouched (5 fields). However I only want to allow change in the last 2 fields.
I am lost, please help. Thanks :)
If you are using the MVC ability to populate your entity/class i.e. your action sig looks like this:
ViewResult MyAction(MyObject object) {
...
Save(MyObject);
}
then you'll need to make sure you include the other field, non-editable, either as visible information or using Html.Hidden within the form scope to ensure you have a fully populated object. Remember, the web is stateless and the server has no idea which record you were editing unless it has the keys to do so retrospectively.
The other option would be to retrieve the original object (for which you'll still need the primary key) from the database, update the fields from your form data and then submit the changes. We'd need to know the specific error to be able to help further, the code you are using would also be a great help.
Without knowing more I would guess it has something to do with binding null to a non null property in your model. Can you give me more details on the model, the error.
If you are using the default mvc model binder then it will only bind the fields you submit. So either submit as hidden or dont use a model binder and manually map the variable from Request.Form into a copy of the model you pulled form the db.

Resources