limiting edit function in asp.net mvc3 - asp.net-mvc-3

I am currently creating an application which references a database using EF in MVC 3. I have used the scaffolding read/write data and want to be able to allow some editing to be done on database entries, but not all fields. Is there a way of blocking a user from editing some fields on the edit form and therefore the database?
Since my initial question I attempted to limit the edit functionality by changing
<div class="editor-label">
#Html.LabelFor(model => model.Clause)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Clause)
#Html.ValidationMessageFor(model => model.Clause)
</div>
to use the
<div class="editor-label">
#Html.LabelFor(model => model.Clause)
</div>
<div class="editor-field">
#Html.DisplayFor(model => model.Clause)
</div>
However when actually saving the edit, the fields which reference the DisplayFor field come out blank. Is there any better way of doing this so it does not occur?
Many thanks,
Chri3

Well, they are quite a few solutions.
You can show only the fields you wanna edit in your view (not secure, a malicious one could add unwanted fields-values to your form).
You can update manually only the fields you want in your controller.
You can use ViewModels, which will take only the Properties you wanna edit (I would advise you to choose that solution).
EDIT
If you want to have the displayed value to be passed in the form, but not "visibly editable", you've got to pass it as an hidden field.
#Html.DisplayFor(model => model.Clause)
#Html.HiddenFor(model => model.Clause)
but that's solution 1. in my answer, probably the worse one.
By the way, if you don't want to edit this field, you should not mind if it comes back blank. Just change the way you update your entities.

Related

How to iterate over a list and create hyperlinks based on the text in thymeleaf?

I am developing a food website using Spring boot, Thymleaf and Bootstrap. I need to display menu items in a webpage. To display it I am fetching the data from a database and then iterating over it to display. However, I am not able to display it in a single column like this. Any suggestions on how to handle this?
Code:
${menu.mainMenuName}"
You need to move your "th:each" statement to the div tag. It will be something like:
<div th:each="menu: ${mainMenu}" class="col-md-12">
<a class="text-info" th:value="${menu.id}" th:text="${menu.mainMenuName}" href=""></a>
</div>
Note that ${menu.mainMenuName}" is redundant with th:text="${menu.mainMenuName}
I found the solution for it. Problem with my previous approach was it was creating col-md-2 for every link hence it was getting displayed in single line to fix it below approach was followed:
<div class="col-md-2">
<div class="main-menu" th:each="menu: ${mainMenu}">
<a class="text-info" th:value="${menu.id}" th:text="${menu.mainMenuName}" href=""></a>
</div>
</div>

knockout checkboxlist with dynamic data

sup guys, my english is not good.. but i'll try my best
I'm new with knockout, I'm really impressed with this tool.
I'm using this framework on my new page in my MVC 3 application. But i just faced a problem on how to mark my checkboxlist with data from the database.
<div data-bind="foreach: listPeople">
<div>
<label>
<input type="checkbox" data-bind="attr: { value: id_person}, checked: $parent.checkedPeople " />
<span data-bind="text: name_person"></span>
</label>
</div>
</div>
as u guys can see, im using the checked tag to "hold" the id_person information to save im my database.
listPeople is an observableArray with my people.
and checkedPeople is an observableArray with those chosed people.
while inserting its working like a piece of cake.
the problem is when im tryin to "edit". When i try to previous populate "checkedPeople".
isnt knockout supposed to recognize it ?
I am trying to understand your question here. Do you mean to say that the list is binding with the people, but doesn't check the chosen people appropriately? If that's the case, your chosenPeople observable array needs to be an array of integers (not of type People).
The value of the checkbox needs to match at least one of the integers in the chosenPeople array for it to appear as checked.

Rails 3.1 Upload images and save them to the database and render them back when needed?

I'm building a Rails Application and i want to store some images to the database and render them back when i need them in the html.erb file.
Can someone point me at the right direction????
Also i have a side bar on my layout file and i want to render something in one controller and then something else in an other controller!
Is that possible?
This is the an exaple layout file but i don't want to render them on the layout
<div id="left_sidebar" class="column">
</div>
<div id="main_wrapper" class="column">
<%= yield %>
</div>
<div id="right_sidebar" class="column">
</div>
Thank you very much in advance!
You should not store image files in your database. I recommend you use gems like Carrierwave.

Zend_Forms and decorators in ZF - Displaying label, input and errors in one container

pretty much ripping my hair out over how badly designed the decorators are in Zend Framework.
I've spent a lot of today and a few other days trying to figure out how to do something that should be a simple frontend task.
Zend_Forms and decorators in my opinion are the worst part of ZF as backend and frontend are not properly seperated. Why is the form class dictating how the HTML should be printed?
All I'm looking for, is something simple like:
<div class="labeledField">
<label for="username">Username</label>
<input type="text" id="username" name="username" />
</div>
<div class="labeledField">
<label for="password">Password</label>
<input type="password" id="password" name="password" />
</div>
The reason I want it like this is because I want the label to sit on top of the field like:
[Username ]
[Password ]
This way I can make the label slightly fade when the input is selected but still remain when the input is selected. Using JS the label is hidden when the input box contains anything. This functionality exists on the Apple shopping cart.
I love the validation parts of Zend_form and I would love how it can be placed in the frontend if backend code wasn't dictating how the HTML looks.
In my opinion it should either take a template (from a frontender with access to the /views/ folder or I should be able to do something like:
<div class="labeledField">
<?php
echo $this->form->username->getLabel();
echo $this->form->username->getElement();
$errors = $this->form->username->getErrors();
if (sizeof($errors) > 0) {
?>
<div class="errors">
<?php
foreach($errors as $error) {
?>
<li><?php echo $error ?></li>
<?php
}
?>
</div>
<?php
}
?>
</div>
But then the templating would allow me to just echo $this->form and have it use the format as above for fields I want to.
No question about it, decorators can take some getting used to. Let me address/answer some of your issues/questions:
pretty much ripping my hair out over how badly designed the decorators are in Zend Framework. I've spent a lot of today and a few other days trying to figure out how to do something that should be a simple frontend task.
Yep, it's a miracle I have any hair left at all.
Zend_Forms and decorators in my opinion are the worst part of ZF as backend and frontend are not properly separated. Why is the form class dictating how the HTML should be printed?
I agree with you here that how the form is to be rendered is a presentation issue, so it's probably more view-related. If you really wanted to separate the concerns, you could create your forms undecorated and then add decorators in your view-script, perhaps using some custom view-helpers. This makes the most sense to me, though I confess I have never bothered to do it.
For examples of a standalone class that sets decorators on a separate form, check out EasyBib_Form_Decorator and Using Zend_Form without Zend Framework MVC
In my opinion it should either take a template (from a frontender with access to the /views/ folder or I should be able to do something like:
You pretty much do have that functionality using the ViewScript decorator.
$form->setDecorators(array(
array('ViewScript', array( // note case
'viewScript' => '_partials/forms/my.phtml', // note case
)));
That said, your desired markup is relatively straightforward using standard decorators:
// not tested, but should be pretty close
$element->setDecorators(array(
'ViewHelper',
'Label',
'Errors',
array('HtmlTag', array('tag' => 'div', 'attribs' => array('class' => 'labeledField'))),
));
Of course, any special styling or client-side manipulation of the form - for example, the Apple cart effect you cite - can be layered on to this markup on the client-side.

EditorFor can't post MVC 3

I've come across a peculiarity with EditorFor() helper in MVC 3.
I have a form view that is strongly type (trimmed down):
#model GoGoLegal.Models.Address
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<div>
#Html.EditorFor(model => model)
</div>
<div>
<input type="submit" value="Post" />
</div>
}
and I have an Address EditorTemplate.
When the Input Button is click without anything in the fields then Validation gets thrown, however when there are values in the fields and the Input Button is clicked then nothing happens.. at all... It doesn't hit the controller, both HttpWatch and FireBug don't even register the event. I'm wondering whats happen.
I've also tried to replace
#Html.EditorFor(model => model)
with
#Html.EditorForModel(Model)
And still the same thing.
Any thoughts on this?
Have you tried clearing your cache. Sometimes a cached version of the page gets confused. Try Ctrl-F5 after you've loaded the page, then see if the page posts.
Also, look at your code and make sure there aren't any malformed tags. That can also confuse the browser, maybe you don't close everything.
Also check the actual HTML in the browser with a "View source" and see if the HTML looks correct.

Resources