i have a poll in my web site and each poll have dynamic answer that this answer save to database i want to edit this answer
Answer an = new Answer();
var ans = an.ReadByByQid(Int64.Parse(ViewData["ID"].ToString()));
<div class="editor-label">
Answer
</div>
<div class="editor-field">
#foreach (var item in ans)
{
<input type="text" name="answer" value="#item.answere" />
}
</div>
i want get id of answer in controller to edit answer in database
Save the id in a hidden input and your form post data will have the answerId and you can access it in the action the same way you access the answer.
Answer an = new Answer();
var ans = an.ReadByByQid(Int64.Parse(ViewData["ID"].ToString()));
<div class="editor-label">
Answer
</div>
<div class="editor-field">
#foreach (var item in ans)
{
<input type="text" name="answer" value="#item.answere" />
<input type="hidden" id="answerId" value="#ans" />
}
</div>
Related
Hey anyone can you tell me the mistake??
this checkbox i show it from my table genre
<div class="row form-group">
<div class="col col-md-3"><label class=" form-control-label">Genre Film</label>
</div>
<div class="col col-md-9">
#foreach($genre as $gr)
<div class="form-check">
<div class="checkbox">
<label for="checkbox1" class="form-check-label ">
<input type="checkbox" name="genre[]" value="{{ $gr->nama_genre }}"> {{ $gr->nama_genre }}
</label>
</div>
</div>
#endforeach
</div>
and then this is the form
enter image description here
controller
public function tambah_movlist(Request $request)
{
$movielist = new Movielist();
$movielist->poster = $poster;
$movielist->judul =$request->judul;
$movielist->tahun =$request->tahun;
$movielist->genre[] = ($request->genre);
$movielist->rating =$request->rating;
$movielist->biaya_produksi =$request->b_produk;
$movielist->pendapatan = $request->pendapatan;
$movielist->sinopsis =$request->sinopsis;
$movielist->save();
return redirect('/adminmovielist');
}
In the HTML, you defined genre value as an array so you can directly get genre value through $request->genre and can save it in the database. Actually no need array_keys().
Please change code:
$movielist->genre[] = ($request->genre);
to
$movielist->genre = $request->genre;
And check the data type of column genre in the database is it right
I have a Ajax.BeginForm call that is supposed to return a partial view but is rerouting the page to the Action instead. Any ideas on what is wrong?
Here is the code on the main page that I want to render the partial view on:
<div class="col-md-6">
#using (Ajax.BeginForm("Search", "Home", new AjaxOptions
{
HttpMethod = "GET",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "searchResults"
}))
{
<div class="form-group" style="width:85%;">
<div class="right-inner-addon">
<i class=" glyphicon glyphicon-search"></i>
<input type="text" data-autocomplete="#Url.Action("Quicksearch","Home")" class="form-control" placeholder="Search" name="q" />
</div>
</div>
<div class="form-group">
<button class="btn btn-default form-inline" type="submit">Search</button>
</div>
}
<br />
</div>
</div>
<div id="searchResults">
</div>
Here is the Partial view (items removed due to length):
<div class="row" id="searchResults">
...removed form elements
<div class="row">
<table class="table">
....stuff
</table>
</div>
</div>
Here is the controller:
public PartialViewResult Search(string q)
{
var items = db.items_with_descriptions
.Where(r => r.name.Contains(q) || string.IsNullOrEmpty(q))
.Take(10);
return PartialView("_Items", items);
}
As stated above, when I click my search button it redirects to localhost:24942/Home/Search instead of staying on the page and simply loading the partial view. I am new to MVC, so please keep that in mind. :)
The jquery-unobtrusive js file has to be included in your page to make the ajax.beginform work
Can someone please tell me the code I need, so that this form updates the database without needing a refresh?
#{
Layout = "~/_template1.cshtml";
var db = Database.Open("stayinflorida");
var CurrentUser = WebSecurity.CurrentUserId;
var userdetails = ("SELECT * from UserProfile WHERE UserId='8'");
var quserdetails = db.QuerySingle(userdetails, CurrentUser);
if (IsPost){
var updateuserdetails = "UPDATE UserProfile SET FirstName = #0, LastName = #1 WHERE UserID='8'";
db.Execute(updateuserdetails, Request["FirstName"], Request["LastName"]);
}
}
<h1>My Details</h1>
<hr>
<form method="post" action="~/Account/MyDetails.cshtml">
<fieldset>
<label>First Name</label>
<input class="input-xlarge" type="text" name="FirstName" placeholder=".input-xlarge" value="#quserdetails.FirstName">
<br>
<label>Last Name</label>
<input class="input-xlarge" type="text" name="LastName" placeholder=".input-xlarge" value="#quserdetails.LastName">
<button type="submit" class="btn btn-success">Update</button>
<button type="submit" class="btn btn-success">Cancel</button>
</fieldset>
</form>
What I have written kind of works, but I have to hit F5 for it to update, and it's just not what I want. I want to use jQuery/ajax, but I just don't know the code.
You need some JavaScript if you want to use AJAX. The jQuery library simplifies this. Here's an article I wrote that features AJAX updates using WebMatrix: http://www.mikesdotnetting.com/Article/176/WebMatrix-and-jQuery-Forms-Part-2-Editing-Data
hi I'm creating a project where the user all the user are running off the same instance using the singleton design pattern. when user types a value into a textbox. It will then wait until everyone else has type a value and then display the results. My question is can this be done using buttons that send a value to the model instead of a textbox. e.g. multiple buttons each one sending a different value.
Heres my code now.
view
#{
ViewBag.Title = "Voting";
}
#using (Html.BeginForm())
{
<fieldset>
<div class="editor-label">
What is you vote?
</div>
<div class="editor-field">
#Html.TextBox("Vote")
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
controller
public ActionResult PlanVote()
{
return View();
}
[HttpPost]
public ActionResult PlanVote(int vote)
{
PlanModel myModel = PlanModel.Instance;
myModel.Vote(vote);
if (PlanModel.Instance.currentstate == PlanState.displaying)
{
return RedirectToAction("Index", "Plan");
}
else
{
return RedirectToAction("Waiting", "Plan");
}
}
model
public void Vote(int Votes)
{
countVotes++;
ivote.Add(Votes);
if (countVotes >= iCount)
{
currentstate = PlanState.displaying;
}
}
I'm still Quite new to MVC and would be great-full for any help thanks
Just remove stand alone submit button and use one submit per vote
#using (Html.BeginForm())
{
<fieldset>
<div class="editor-label">
What is you vote?
</div>
<div class="editor-field">
<input type="submit" name="vote" value="1" />
<input type="submit" name="vote" value="2" />
<input type="submit" name="vote" value="3" />
</div>
</fieldset>
}
I create view by model Article.
class Article
{
public string Title {get;set;}
public List<string> Terms {get;set}
}
Terms - can be any count, and I want that they can be added gradually
#using (Html.BeginForm("Create"))
{
#Html.ValidationSummary(true)
<fieldset>
<legend>CreateArticle</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Title)
#Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-label">
Terms:
</div>
<div id="divList">
</div>
#using (Ajax.BeginForm("Update", new AjaxOptions() { Confirm = "Add", HttpMethod = "Post", UpdateTargetId = "divList", InsertionMode = InsertionMode.InsertAfter }))
{
#Html.Partial("_TermPP", "")
<input id="count" name="count" type="hidden" value="-1" />
<input type="submit" value="add" onclick="javascript:plus()" />
}
</fieldset>
}
_TermPP:
#model String
<div>
<input type="text" name="terms[#(ViewBag.Count==null?0:ViewBag.Count)]" value="#(Model == null ? "" : Model)" /> </div>
when the click is sent to a form of ADD but I need to create on the Update. How do this?
You may take a look at the following blog post. Also please note that you cannot nest 2 <form> elements as you did in your code - this is invalid HTML and might result in undefined behavior.