c# - Sending a phrase (variable) from searcher (from view) to controller, asp.net mvc -
i'm trying create searcher in asp.net. i'm green it. i'm trying create in view , send controller variable, has text written in searcher. in moment, have smth --> question is, , how create , send variable , give data written in searcher?
layout
form class="navbar-form navbar-left" role="search"> @using (html.beginform("index", "searcher", formmethod.post, new { phrase = "abc" })) { <div class="form-group"> <input type="text" class="form-control" placeholder="wpisz frazę..."> </div> <button type="submit" class="btn btn-default">@html.actionlink("szukaj", "index", "searcher")</button> } </form>
controller
public class searchercontroller : applicationcontroller { [httpget] public actionresult index(string message) { viewbag.phrase = message; getcurrentuser(); return view(); } }
view
@{ viewbag.title = "index"; } <h2>index</h2> <ul> <li>@viewbag.message</li> </ul>
you're missing key part of mvc -> model.
let's create 1 first:
public class searchmodel { public string criteria { get; set; } }
then let's update "layout" view (don't know why had form in form?):
@model searchmodel @using (html.beginform("index", "searcher", formmethod.post, new { phrase = "abc" })) { <div class="form-group"> @html.editorfor(m => m.criteria) </div> <button type="submit" class="btn btn-default">@html.actionlink("szukaj", "index", "searcher")</button> }
then action serves view:
[httpget] public actionresult index() { return view(new searchmodel()); }
then post method be:
[httppost] public actionresult index(searchmodel model) { viewbag.phrase = model.criteria; getcurrentuser(); return view(); }
Comments
Post a Comment