MVC (in SItefinity) - model-view-controller

In my View I have something like:
#model CreatorModel
<form name="myForm" action="/Creator/CreateAction" enctype="multipart/form-data" id="myForm" method="post">
<div class="a">
<h2 class="a">....</h2>
<div class="b">
<br />
Select a file for a <input type="file" name="aFile" id="aFile" />
<br /><br />
Select a file for b<input type="file" name="bFile" id="bFile" />
<br /><br />
<input type="submit" id="CreateAction" name="CreateAction" value="CreateAction" />
</div>
</form>
But I have an error : File /Creator/CreateAction is not found (CreateAction is an action from CreatorController)
What can I enter as action in <form...> in order to find action from controller?
When I'm using this syntax:
#using (Html.BeginForm("FileUpload", "Board", FormMethod.Post, new { enctype = "multipart/form-data" })) { <input type="file" /> <input type="submit" /> }
I also have an error: A reference is not set to an instance of an object"
Maybe there is a bad routing?

when using MVC widgets in Hybrid mode (on a webforms template) you need to use a special version of BeginForm (because webforms only allows one form on the page).
#Html.BeginSitefinityForm()
should be the form you use to define a form area in your widget. Replace the regular one with that and the widget should work as expected.
I hope this is helpful!

Related

Get all content of POST HTML form in Gin

I've an HTML form:
<body>
<div>
<form method="POST" action="/add"name="submitForm">
<label>Message</label><input type="text" name="message" value="" />
<input type="checkbox" name="complete" value=""> Complete<br>
<input type="submit" value="submit" />
</form>
</div>
</body>
Now I want to access both values (message and complete) at the same time when a POST occurs.
How can I do this in Go (gin gonic)?
I can do it in two times by using c.PostForm but is there a way to collect them both?:
func AddTodoHandler(c *gin.Context) {
fmt.Println(c.PostForm("message"))
fmt.Println(c.PostForm("complete"))
I tried fmt.Println(c.PostForm("submitForm")) but that did not work.
I don't know if you've already solved this, but the way I managed to capture the 2 values simultaneously was by removing the value from the checkbox and using the Request.PostForm instead of the PostForm.
I tried using the form's name or id, but it seems that it's still not possible to get the data from them.
<body>
<div>
<form method="POST" action="/add" name="submitForm"> << not work
<label>Message</label><input type="text" name="message" value="" />
<input type="checkbox" name="complete"> Complete<br> << remove value attribute
<input type="submit" value="submit" />
</form>
</div>
</body>
func AddTodoHandler(c *gin.Context) {
fmt.Println(c.Request.PostForm)
Note: as my reputation is less than 50pts, I cannot comment on the post.

Submit Button with multiple fieldsets on one view

I have a view that has the ultimate aim of being a user creation form. It is structured in the following way:
<fieldset>
#Html.DropDownList("first dropdown")
</fieldset>
<fieldset id="exists">
#Html.DropDownList("seconddropdown")
#Html.DropDownList("third dropdown")
#Html.DropDownList("fourth dropdown")
<input type="submit" value="Create User"/>
</fieldset>
<fieldset id="new">
#Html.DropDownList("dropdown")
#Html.DropDownList("dropdown")
#Html.DropDownList("dropdown")
<input type="submit" value="Create User"/>
</fieldset>
<script>
//script here shows and hides fieldsets depending on value chosen in first dropdown
</script>
Depening on the value chosen in the first dropdown, either fieldset exist or fieldset new will show. they both interact with the database differently so I really need to be able to handle the submit action separately for each. I have tried creating a submit within each fieldset, but the controller action is never hit.
I am obviously missing the mark with MVC submit buttons so could anyone help me understand how these work and how to create multiple (and separate) submit possibilities on one view.
Many thanks
You could have two forms, each that post to a different action...the submit will then only submit the form values in that form...
e.g.
#using (Html.BeginForm("Login", "Account", FormMethod.Post))
{
<fieldset id="exists">
#Html.DropDownList("seconddropdown")
#Html.DropDownList("third dropdown")
#Html.DropDownList("fourth dropdown")
<input type="submit" value="Log in"/>
</fieldset>
}
#using (Html.BeginForm("Create", "Account", FormMethod.Post))
{
<fieldset id="new">
#Html.DropDownList("dropdown")
#Html.DropDownList("dropdown")
#Html.DropDownList("dropdown")
<input type="submit" value="Create"/>
</fieldset>
}

ruby forms in div elements

I'm having an issue where the form generated with ruby isn't included inside my #content div, is this because the form is generated after the html is read by the browser (sorry if I sound like a moron on this)
-- edit update - view source --
The code below generates with the email box and submit button outside of the content box
<div id="content">
<!-- text here -->
<form accept-charset="UTF-8" action="/password_resets" method="post">
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="✓" />
<input name="authenticity_token" type="hidden" value="afmtpSAc93w1uMcnouhY9XmbVTM7fE1VNFvZKnp0kMs=" />
</div>
<div class="field">
<input id="email" name="email" placeholder="email#website.com" type="text" />
</div>
<div class="actions">
<input name="commit" type="submit" value="Reset Password" />
</form> </div>
</div>
I'm not sure I got the question right, but in the html above the last </form> </div> should be </div> </form>
figured it out after visiting this link
Is it correct to use DIV inside FORM?
once I removed the div's for field and action it cleared everything up

Change the CSS class of an input on validation error with razor

In my ASP MVC 3 application, I have this form
#using (Html.BeginForm())
{
<input id="Username" name="UserName" type="text" value="Username" class="form-text" />
<input id="PasswordTxt" name="PasswordTxt" type="text" value="Password" class="form-text" />
<input id="Password" name="Password" type="password" class="form-text" style="display: none"/>
<input id="bt_login" type="submit" value="Log in" class="bt_login" />
<div class="login_lbl_error">
#Html.ValidationSummary()
</div>
}
I want to change the class of each wrong text field to "login_lbl_error".
Any ideas ?
Thanks.
With MVC3, an input-validation-error CSS class will automatically be added to to the input elements which have validation errors.
Therefore in your CSS you can style this class:
.input-validation-error
{
color:red;
}
By default MVC adds input-validation-error and field-validation-error, you can use JQuery to override these classes:
<script type="text/javascript">
$(document).ready(function(){
$('.input-validation-error').addClass('CustomErrorClass').removeClass('input-validation-error');
$('.field-validation-error').addClass('CustomErrorClass').removeClass('field-validation-error');
});
</script>
Since ASP.NET MVC adds the field-validation-error class to the error message element, and input-validation-error to form controls, I just changed the class name using jQuery:
$(".input-validation-error").toggleClass("input-validation-error newClassName");

Duplicate input ids when using multiple partials in ASP.NET MVC

I have three partial views that are each strongly typed with separate models. Each view contains it's own form and submits to different actions. Some of the models contain properties with the same names and when I use the html helper methods to create the textboxes and labels, I'm ending up with duplicate html ids on the page.
Partial _Residential View
#model MyProject.Models.ResidentialModel
#using (Html.BeginForm("Residential", "Transaction"))
{
#Html.LabelFor(m => m.PersonName)
#Html.TextBoxFor(m => m.PersonName)
#Html.LabelFor(m => m.ReferenceNumber)
#Html.LabelForm(m => m.ReferenceNumber)
<input type="submit" value="Submit" />
}
Partial _Business View
#model MyProject.Models.BusinessModel
#using (Html.BeginForm("Business", "Transaction"))
{
#Html.LabelFor(m => m.BusinessName)
#Html.TextBoxFor(m => m.BusinessName)
#Html.LabelFor(m => m.ReferenceNumber)
#Html.LabelForm(m => m.ReferenceNumber)
<input type="submit" value="Submit" />
}
Normal View
<h2>Residential Transaction</h2>
#Html.Partial("_Residential")
<h2>Business Transaction</h2>
#Html.Partial("_Business")
The output that I'm getting looks like the following:
<h2>Residential Transaction</h2>
<form action="/Transaction/Residential" method="post">
<label for="PersonName">Person Name:</label>
<input type="text id="PersonName" name="PersonName" />
<label for="ReferenceNumber">Reference Number:</label>
<input type="text" id="ReferenceNumber" name="ReferenceNumber" />
<input type="submit" value="Submit" />
</form>
<h2>Business Transaction</h2>
<form action="/Transaction/Business" method="post">
<label for="BusinessName">Business Name:</label>
<input type="text" id="BusinessName" name="BusinessName" />
<label for="ReferenceNumber">Reference Number:</label>
<input type="text" id="ReferenceNumber" name="ReferenceNumber" />
<input type="submit" value="Submit" />
</form>
Since ReferenceNumber is in both models, I'm getting a duplicate id on the page. I figured out that i can pass an extra htmlAttributes parameter to TextBoxFor to change the id.
#Html.TextBoxFor(m => m.ReferenceNumber, new { id = "ResidentialReferenceNumber" })
...
#Html.TextBoxFor(m => m.ReferenceNumber, new { id = "BusinessReferenceNumber" })
This fixes the ids on the inputs but the 'for' attribute on the labels still has the wrong value. I looked through the various overloads for LabelFor() and unlike the TextBoxFor, there isn't an htmlAttributes property. Is there some way that I can tell model or view to use a different id for these fields?
MVC helpers do not take into account that you can have multiple models in a view. All properties that have the same name will get duplicate values (one value per model).
That is, if ResidentalTransaction and BusinessModel has a Name property, your html form will have two <input type="text" name="Name" /> fields.
I'm unsure of how it works if you add all models to the same viewmodel and use Html.TextBoxFor(m => m.ResidentalTransaction.Id);

Resources