I am beginner in MVC3 and building one application, i need to make confirmation page where all details of user will display for confirmation.
I have build wizards to fill this information using javascript & divs and in final wizard i would like to put all details which have been filled by user
#using (Html.BeginForm("Confirm", "Home", FormMethod.Get))
{
<div>
//user details goes here...
</div>
<input type="submit" name="name" value="confirm" />
}
how can i load data here?, i need to make some method call before form will render or something else? please guide me.
thanks in advance.
The best way would be to load those data in action that return this page. Load user data into a model object and pass that object to a view:
// Return view
return View( new SomeViewModel(userData));
And than you handle those data in view, like (Razor):
#this.Model.UserFirstName ...
#using (Html.BeginForm("Confirm", "Home", FormMethod.Get))
{
<div id="userDetailDiv">
</div>
<input type="submit" name="name" value="confirm" />
}
you can use .load
$(function(){
$("#userDetailDiv").load(#Url.Action('UserDetail'));
}
or you can use RenderAction
#using (Html.BeginForm("Confirm", "Home", FormMethod.Get))
{
<div id="userDetailDiv">
#Html.RenderAction("_UserDetail", "Cintroller");
</div>
<input type="submit" name="name" value="confirm" />
}
Related
I have an existing ASP.NET app that uses one Razor component throughout. Unfortunately, this component does not have a model associated with it. I'm in a scenario where I need to add one parameter. At this time, I have the following in the component host view:
#await Component.InvokeAsync("MyTextField", new { Align = "Left" })
The component Razor code currently looks like this:
MyTextField.cshtml
<div class="text-right">
<form asp-action="ReadItem" asp-controller="Inventory" method="get" id="inventory-form">
<input id="inputField" class="input-text" type="text" />
<button class="submit-button" type="submit">Submit</button>
</form>
</div>
Currently, the component renders. However, I want to get the value of the Align parameter, if it exists, in the MyTextField.cshtml view. Is there a way for me to get a parameter value there? If so, how?
Thanks
You can do that in this way:
#await Component.InvokeAsync("MyTextField", new User{ Align = "Left" })
and in View (cshtml file):
#using UserNamespace
#model User
<div class="text-right">
<form asp-action="ReadItem" asp-controller="Inventory" method="get" id="inventory-form">
<input id="inputField" class="input-text" type="text" value="#Model.Align" />
<button class="submit-button" type="submit">Submit</button>
</form>
</div>
Sorry I am quite new in ASP.NET MVC and I have this piece of code in the Home view folder (file Index.cshtml):
#{
ViewBag.Title = "Home Page";
}
<div class="row">
<div class="col-md-4">
<h2>Select file</h2>
<p>
<input id="File1" type="file" />
</p>
<p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301865">Learn more ยป</a></p>
</div>
<div class="col-md-4">
<h2>Upload to SQL Server</h2>
<input type="button" title="Upload to SQL Server" value="Upload to SQL Server" onclick="location.href='#Url.Action("Upload2SS", "SystemLogs")'" />
</div>
</div>
When I click the "Upload to SQL Server" button, I simply want to pass the File1 value to the controller action:
public RedirectToRouteResult Upload2SS(FormCollection form)
{
string filePath = form["File1"].ToString();
var data = GetDataTabletFromCSVFile(filePath);
return RedirectToAction("Index");
}
However, I keep on getting a System.NullReferenceException for the filePath variable; can anyone tell me what I am missing please?
Your code missing a lot of things. First of all you need to use a form tag in which you will have input file and input button.
This form will help you to submit bunch of information to the server. Similarly you need to set the name attribute, From your code you are missing name attribute in your input file.
You can search Stackoverflow and find any answer. For submitting file, read this answer https://stackoverflow.com/a/28380690/713789
I am trying to call an action from the controller using onclick method. For some reason it's not returning action I want, it's always jumping to public ActionResult Index() by default.
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Register" class="btn btn-default" onclick="location.href='#Url.Action("RegisterIndex", "Register")'"/>
</div>
</div>
NOTE
I have created view automatically with a model. It's validating input for me by using already generated javascripts. If I change input tag to button it's not gonna do the required validation.
window.location.href does a GET request, that's why it didn't pass your input values to the server.
When you have <input type="submit"> inside a form, clicking it will submit the form with all data you need. I think this is what you want, but you just want it to submit to another action.
To achieve this, I suggest this solution:
Create a hidden field in the form. Its data will be sent to the server.
In your server, base on that hidden value, you can redirect to the appropriate action
Please feel free to ask me if you find anything unclear :)
The <input type="submit">, when inside a form element, will submit the form when clicked unless you return false or event.preventDefault();
returning false will prevent the default behavior for your submit.
EDIT
window.location.href will cause a GET request so your data will not be posted using this method.
HTML
#using (Html.BeginForm())
{
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" id="btnSubmit" value="Register" class="btn btn-default"/>
</div>
</div>
}
Javascript
<script>
$(document).ready(function () {
$("#btnSubmit").click(function () { window.location.href = '#Url.Action("RegisterIndex", "Register")'; return false; });
});
</script>
I've started with asp mvc 3, with c# and razor, then. I want to use forms with security for send petitions POST.
I want to with razor render some like that
<form action="/sass/" method="post">
#using (Html.BeginForm()) {
#Html.AntiForgeryToken()
<div class="form-group">
<label>Ingresa tu Nombre</label>
<input class="form-control" name="nombre" />
</div>
<div class="form-group">
<input type="submit" value="Enviar mi duda" class="btn btn-primary btn-sm" />
</div>
}
And in C# I dont know how to validate that csrf token, is valid.
I work with C#, asp mvc3 and razor.
Please help me!
In your action method you need to add the respective attribute [ValidateAntiForgeryToken], and it validate the input for you.
You have a problem with the state of your code. There are two embedded forms: the outer one and the one produced by Html.BeginForm. However, the way to validate the token is to decorate the target action or controller with [ValidateAntiForgeryToken].
So either:
[ValidateAntiForgeryToken]
public ActionResult Index()
{
return View();
}
or to validate all methods in the controller:
[ValidateAntiForgeryToken]
public class MyController : Controller
{
}
I'll admit I'm new to MVC and this question might be a single case of RTFM. But I'm googling this problem and I can't seem to find a solution.
I've got a simple view used to fill out some details for a specific model. I need to render part of the form using Html.Partial (in truth this is a wrapper which renders old non-MVC controls used from another project).
I've no problems getting data FROM the controller INTO the view.
So what's the issue? How do I get user input from the partial view back to the controller after the user pressed the submit button?
Here's the view and controller I've currently got:
#model Poll
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Poll</h4>
<hr />
#*#Html.ValidationSummary(true)*#
#Html.HiddenFor(m => m.Id)
#Html.HiddenFor(m => m.Name)
#Html.Partial("~/ControlPlaceholder/QuestionPlaceholder.ascx", Model, new ViewDataDictionary(Model))
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Fill" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
The view has been copied almost one-to-one from the standard generated edit view available in MVC5. Note that this is currently just a PoC - normally the whole thing should render a QuestionPlaceholder for every question in a Poll.
Here's the relevant part of the controller:
//
// GET: /Poll/Fill
[HttpGet]
public ActionResult Fill(Guid id)
{
var poll = pollRepository.Get(id);
return View(poll);
}
//
// POST: /Poll/Fill
[HttpPost]
public ActionResult Fill(Poll poll, FormCollection collection)
{
try
{
return RedirectToAction("Index");
}
catch
{
return View(poll);
}
}
it is so simple, just set name of inputs same as corresponding action parameters and let MVC ModelBinder do it's job. it's not important to render a partial in the form, it's input elements value would be passed to the action on submitting form.
another way is to use Request.Form["InputName"] that is not my first recommendation.