MultiSubmitButton Clientside validation - asp.net-mvc

I have created one View. In that i have put 2 Multisubmit buttons for "create" and "cancel".
Now, on cancel click, clientside validation is going to call. but I don't want to call clientside validations.
I have written code as below, please check it. I don't want validations to be executed on cancel button click.
#model Blog.Models.User
#{
ViewBag.Title = "Register New User";
}
#using Microsoft.Web.Mvc;
#using MVC3MultiSubmitButtonExtension;
<h2>
Register New User</h2>
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<fieldset>
<legend>Fill User Details</legend>
<div class="editor-label">
#Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.FirstName)
#Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.LastName)
#Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Username)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Username)
#Html.ValidationMessageFor(model => model.Username)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Password)
#Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.EmailAddress)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.EmailAddress)
#Html.ValidationMessageFor(model => model.EmailAddress)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.MobileNumber)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.MobileNumber)
#Html.ValidationMessageFor(model => model.MobileNumber)
</div>
<p>
#* <input type="submit" value="Create" />*#
#Html.MultiSubmitButton(Url.Action("Create"), "btnCreate", "Create")
#Html.MultiSubmitButton(Url.Action("Cancel"), "btnCancel", "Cancel")
</p>
</fieldset>
}
<div>
#Html.ActionLink("Click here to go Login Page", "Index")
</div>

If you assign class="cancel" to your cancel submit button, clientside validation won't run. The way you assign this class is not something that I can tell as you seem to be using some custom helper Html.MultiSubmitButton which is not part of ASP.NET MVC. So you will have to check its documentation.

Related

What is the VB.NET equivalence of this razor syntax?

I'm really having issues with the lambda expressions.
#model CalculateSimpleInterest.Models.SimpleInterestModel
#{
ViewBag.Title = "SimpleInterest";
}
<h2>Calulate Simple Interest</h2>#using (Ajax.BeginForm("CalculateSimpleInterestResult","CalculateSimpleInterest",
new AjaxOptions { UpdateTargetId = "divInterestDeatils" }))
{
<fieldset>
<legend>Calulate Simple Interest</legend><div id="div1"></div>
<div class="editor-label">
#Html.LabelFor(model => model.Amount)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Amount)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Rate)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Rate)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Year)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Year)
</div>
<p>
<input type="submit" value="Calculate" />
</p>
</fieldset>
}
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
The syntax you are looking for is
#Html.YourHelperFor(Function(model), model.Property)
eg
<div class="editor-label">
#Html.LabelFor(Function(model), model.Amount)
</div>

ASP MVC3 how to add system current date?

I have form like
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Course</legend>
#Html.HiddenFor(model => model.CId)
<div class="editor-label">
#Html.LabelFor(model => model.CName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.CName)
#Html.ValidationMessageFor(model => model.CName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Cteator)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Cteator)
#Html.ValidationMessageFor(model => model.Cteator)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.date)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.date)
#Html.ValidationMessageFor(model => model.date)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
I want to insert system current date and time in the field of Date field. How can i do this. Model and cotroller are already create by scaffolding.
Insert this before you call the editor (assuming your property is a DateTime)
#{ Model.date = DateTime.Now; }
If your property is a string, call DateTime.Now.ToString().
You could set the default value in the corresponding controller action:
public ActionResult Index() {
return View(new SomeModel { date = DateTime.Now });
}
Change your code as mention below
#{ Model.date = DateTime.Now.ToString(); }
#Html.ValidationSummary(true)
<fieldset>
<legend>Course</legend>
#Html.HiddenFor(model => model.CId)
<div class="editor-label">
#Html.LabelFor(model => model.CName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.CName)
#Html.ValidationMessageFor(model => model.CName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Cteator)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Cteator)
#Html.ValidationMessageFor(model => model.Cteator)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.date)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.date)
#Html.ValidationMessageFor(model => model.date)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>

get the room by id

I am working on a hotel Reservation Project in MVC 3
Here I display the rooms:
When i click to reserve a romm i get here :
But what i want to do is to get the number of the room i selected directly in the Reservation form room box.
Here is my Create view for reservation:
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Reservation</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Room_ID, "Room")
</div>
<div class="editor-field">
#Html.DropDownList("Room_ID", String.Empty)
#Html.ValidationMessageFor(model => model.Room_ID)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Guest_ID, "Client")
</div>
<div class="editor-field">
#Html.DropDownList("Guest_ID", String.Empty)
#Html.ValidationMessageFor(model => model.Guest_ID)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Data_Check_in)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Data_Check_in)
#Html.ValidationMessageFor(model => model.Data_Check_in)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Data_Check_out)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Data_Check_out)
#Html.ValidationMessageFor(model => model.Data_Check_out)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Preference_ID, "Preferinte")
</div>
<div class="editor-field">
#Html.DropDownList("Preference_ID", String.Empty)
#Html.ValidationMessageFor(model => model.Preference_ID)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Price)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Price)
#Html.ValidationMessageFor(model => model.Price)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Card_ID, "Card_de_credit")
</div>
<div class="editor-field">
#Html.DropDownList("Card_ID", String.Empty)
#Html.ValidationMessageFor(model => model.Card_ID)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
Can you pass the room id from the first page to the form page using a view model?

How can insert fix value in a field when creating a new row

I am working on asp.net mvc3. I have designed database in SQL Server. And I access database using ado.net, I am designing a "CREATE" page.
This is my view.cshtml
#model CalcoWOMS.Models.ProductFormulation
#{
ViewBag.Title = "doProductFormulation";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>ProductFormulation</legend>
<div>
#Html.EditorFor(model => model.ProductID) <!-- i want to insert fix value (100) in this ProductID field in ProductFormulation Table -->
#Html.ValidationMessageFor(model => model.ProductID)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.RawMaterialID)
</div>
<div class="editor-field">
#Html.DropDownList("RawMaterialID","SELECT")
#Html.ValidationMessageFor(model => model.RawMaterialID)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Code)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Code)
#Html.ValidationMessageFor(model => model.Code)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Description)
#Html.ValidationMessageFor(model => model.Description)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.MinimumBatchSize)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.MinimumBatchSize)
#Html.ValidationMessageFor(model => model.MinimumBatchSize)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Quantity)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Quantity)
#Html.ValidationMessageFor(model => model.Quantity)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "ProductFormulationIndex")
</div>
I want to insert fixed value (100) in this ProductID field in ProductFormulation table and don't want to create editable box. What should I
do for this ?
Perhaps I don't understand the question (I'm more of a backend developer), but it seems like the simple solution is remove these two lines:
<div>
#Html.EditorFor(model => model.ProductID)
#Html.ValidationMessageFor(model => model.ProductID)
</div>
Then update the Model in your controller's Create, [post] method to update the ProductID to 100, prior to writing it out to the repository. Of course, how you'd do that depends on your controller structure, which you haven't posted.
As I understand it, the alternative would be to have a hidden field on the page and create the Model with a ProductID of 100 in the initial Create call but if you're always going to hard code the product id (which to be honest seems questionable) to 100, then this seems pointless...

Master-Detail Sample Code for MVC 3 Razor (using Ajax for details) - Part 2

I am currently showing a treeview in a left column.
When a user selects a node, i am loading the right column #details with the response from an ajax call. This works nicely.
I now want to submit the right columns #details via ajax as well. I am able to capture the post back to the server however when i return a string back to the #details section it is loading the entire page with string 'saved successfully'
I really want the string 'saved successfully' to be placed in a div element in the right column.
This is an ajax response, again doing another ajax response (which i want to return to the first ajax response).
Is this possible?
My edit form looks like the following
#using (Ajax.BeginForm("Edit", new AjaxOptions { UpdateTargetId = "#results" })) {
#Html.ValidationSummary(true)
if (Model != null) {
<fieldset>
<legend>Code</legend>
#Html.HiddenFor(model => model.CodeID)
<div class="editor-label">
#Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Description)
#Html.ValidationMessageFor(model => model.Description)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Note)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Note)
#Html.ValidationMessageFor(model => model.Note)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.DateModified)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.DateModified)
#Html.ValidationMessageFor(model => model.DateModified)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.TopicID)
</div>
<div class="editor-field">
#Html.DropDownListFor(model => model.TopicID, (SelectList)ViewData["Topics"], "Select")
#Html.ValidationMessageFor(model => model.TopicID)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Special)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Special)
#Html.ValidationMessageFor(model => model.Special)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Html)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Html)
#Html.ValidationMessageFor(model => model.Html)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
<div id="results">
Status
</div>
Make sure you have included the following script to your page if you want to use the Ajax.* helpers:
<script src="#Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
If you don't include it there is nothing out there to make sense of the HTML5 data-* attributes emitted by the Ajax.* helper and AJAXify this form => the form performs a normal submit and renders the results.

Resources